From 46a8a560e80dd9f520fb00cbbe9fdb69d9183afb Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:19:43 +0100 Subject: [PATCH 1/9] Comment the function --- datasetUtils/checkForServiceAvailability.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/datasetUtils/checkForServiceAvailability.go b/datasetUtils/checkForServiceAvailability.go index 33cc691..c7fa4b2 100644 --- a/datasetUtils/checkForServiceAvailability.go +++ b/datasetUtils/checkForServiceAvailability.go @@ -28,7 +28,10 @@ type ServiceAvailability struct { Qa OverallAvailability } +// CheckForServiceAvailability checks the availability of the dataset ingestor service. +// It fetches a YAML file from a specified location, parses it, and logs the service availability status. func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarchiveFlag bool) { + // Send a GET request to fetch the service availability YAML file resp, err := client.Get(DeployLocation + "datasetIngestorServiceAvailability.yml") if err != nil { fmt.Println("No Information about Service Availability") @@ -36,23 +39,28 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch } defer resp.Body.Close() + // If the HTTP status code is not 200 (OK), log a message and return if resp.StatusCode != 200 { log.Println("No Information about Service Availability") log.Printf("Error: Got %s fetching %s\n", resp.Status, DeployLocation + "datasetIngestorServiceAvailability.yml") return } + // Read the entire body of the response (the YAML file) yamlFile, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Can not read service availability file for this application") return } + // Unmarshal the YAML file into a ServiceAvailability struct s := ServiceAvailability{} err = yaml.Unmarshal(yamlFile, &s) if err != nil { log.Fatalf("Unmarshal of availabilty file failed: %v\n%s", err, yamlFile) } + + // Determine the service status and environment based on the testenvFlag var status OverallAvailability var env string // define default value @@ -68,8 +76,11 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch } env = "production" } + + // Reset the terminal color after the function returns defer color.Unset() + // Log the planned downtime for the ingest and archive services, if any if status.Ingest.Downfrom != "" { color.Set(color.FgYellow) fmt.Printf("Next planned downtime for %s data catalog ingest service is scheduled at %v\n", env, status.Ingest.Downfrom) @@ -86,6 +97,8 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch color.Set(color.FgYellow) fmt.Printf("It is scheduled to last until %v\n", status.Archive.Downto) } + + // If the ingest service is not available, log a message and terminate the program if status.Ingest.Status != "on" { color.Set(color.FgRed) log.Printf("The %s data catalog is currently not available for ingesting new datasets\n", env) @@ -93,6 +106,8 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch color.Unset() os.Exit(1) } + + // If the archive service is not available and autoarchiveFlag is set, log a message and terminate the program if autoarchiveFlag && status.Archive.Status != "on" { color.Set(color.FgRed) log.Printf("The %s data catalog is currently not available for archiving new datasets\n", env) From 613bad82a6a743d17060e4ce212ba58c4c34874d Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:57:53 +0100 Subject: [PATCH 2/9] Add tests for reading the YAML file --- datasetUtils/checkForServiceAvailability.go | 57 +++++---- .../checkForServiceAvailability_test.go | 117 ++++++++++++++++++ 2 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 datasetUtils/checkForServiceAvailability_test.go diff --git a/datasetUtils/checkForServiceAvailability.go b/datasetUtils/checkForServiceAvailability.go index c7fa4b2..443fa0e 100644 --- a/datasetUtils/checkForServiceAvailability.go +++ b/datasetUtils/checkForServiceAvailability.go @@ -6,9 +6,8 @@ import ( "log" "net/http" "os" - + // "encoding/json" "gopkg.in/yaml.v2" - "github.com/fatih/color" ) @@ -28,29 +27,15 @@ type ServiceAvailability struct { Qa OverallAvailability } +var GitHubMainLocation = "https://github.com/paulscherrerinstitute/scicat-cli/blob/main" + // CheckForServiceAvailability checks the availability of the dataset ingestor service. -// It fetches a YAML file from a specified location, parses it, and logs the service availability status. +// It fetches a YAML file from GitHubMainLocation, parses it, and logs the service availability status. func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarchiveFlag bool) { - // Send a GET request to fetch the service availability YAML file - resp, err := client.Get(DeployLocation + "datasetIngestorServiceAvailability.yml") - if err != nil { - fmt.Println("No Information about Service Availability") - return - } - defer resp.Body.Close() - - // If the HTTP status code is not 200 (OK), log a message and return - if resp.StatusCode != 200 { - log.Println("No Information about Service Availability") - log.Printf("Error: Got %s fetching %s\n", resp.Status, DeployLocation + "datasetIngestorServiceAvailability.yml") - return - } - - // Read the entire body of the response (the YAML file) - yamlFile, err := io.ReadAll(resp.Body) + yamlFile, err := readYAMLFile(client) if err != nil { - fmt.Println("Can not read service availability file for this application") - return + log.Printf("Failed to read service availability YAML file: %v", err) + return } // Unmarshal the YAML file into a ServiceAvailability struct @@ -116,3 +101,31 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch os.Exit(1) } } + +func readYAMLFile(client *http.Client) ([]byte, error) { + // Construct the URL of the service availability YAML file + yamlURL := fmt.Sprintf("%s/cmd/datasetIngestor/datasetIngestorServiceAvailability.yml", GitHubMainLocation) + + // Send a GET request to fetch the service availability YAML file + resp, err := client.Get(yamlURL) + if err != nil { + fmt.Println("No Information about Service Availability") + return nil, fmt.Errorf("failed to fetch the service availability YAML file: %w", err) + } + defer resp.Body.Close() + + // If the HTTP status code is not 200 (OK), log a message and return + if resp.StatusCode != 200 { + log.Println("No Information about Service Availability") + log.Printf("Error: Got %s fetching %s\n", resp.Status, yamlURL) + return nil, fmt.Errorf("got %s fetching %s", resp.Status, yamlURL) + } + + // Read the entire body of the response (the YAML file) + yamlFile, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("Can not read service availability file for this application") + } + + return yamlFile, nil +} diff --git a/datasetUtils/checkForServiceAvailability_test.go b/datasetUtils/checkForServiceAvailability_test.go new file mode 100644 index 0000000..9626b47 --- /dev/null +++ b/datasetUtils/checkForServiceAvailability_test.go @@ -0,0 +1,117 @@ +package datasetUtils + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + "bytes" + "gopkg.in/yaml.v2" +) + +func TestReadYAMLFile(t *testing.T) { + // Create a mock HTTP server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/repos/paulscherrerinstitute/scicat-cli/releases/latest": + // Respond with a mock release + fmt.Fprintln(w, `{"tag_name": "v1.0.0"}`) + case "/paulscherrerinstitute/scicat-cli/releases/download/v1.0.0/cmd/datasetIngestor/datasetIngestorServiceAvailability.yml": + // Respond with a mock YAML file + fmt.Fprintln(w, "mock: YAML file") + default: + http.NotFound(w, r) + } + })) + defer server.Close() + + // Update the GitHubAPI and GitHubMainLocation variables to point to the mock server + oldGitHubAPI := GitHubAPI + oldGitHubMainLocation := GitHubMainLocation + GitHubAPI = server.URL + "/repos/paulscherrerinstitute/scicat-cli/releases/latest" + GitHubMainLocation = server.URL + "/paulscherrerinstitute/scicat-cli/releases/download/v1.0.0" + defer func() { + // Restore the original variables after the test + GitHubAPI = oldGitHubAPI + GitHubMainLocation = oldGitHubMainLocation + }() + + // Create a new HTTP client + client := &http.Client{} + + // Call the function + yamlFile, err := readYAMLFile(client) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + // Check that the function returned the expected YAML file + expected := []byte("mock: YAML file\n") + if !bytes.Equal(yamlFile, expected) { + t.Errorf("Expected %q, got %q", expected, yamlFile) + } +} + +func TestReadYAMLFileIntegration(t *testing.T) { + // Create a new HTTP client + client := &http.Client{} + + // Call the function + yamlFile, err := readYAMLFile(client) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + // Check that the function returned a non-empty file + if len(yamlFile) == 0 { + t.Errorf("Expected a non-empty YAML file, got an empty file") + } +} + +func TestYAMLStructure(t *testing.T) { + // The test will fail if the indentation of yamlFile is not correct + yamlFile := []byte(` +production: + ingest: + status: on + archive: + status: on +qa: + ingest: + status: on + archive: + status: on +`) + + var serviceAvailability ServiceAvailability + + err := yaml.Unmarshal(yamlFile, &serviceAvailability) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + checkService := func(service Availability, serviceName string) { + if service.Status != "on" && service.Status != "down" { + t.Errorf("Expected status 'on' or 'down' for %s, got %q", serviceName, service.Status) + } + + if service.Status == "down" { + if service.Downfrom == "" { + t.Errorf("Expected 'downfrom' for %s when status is 'down'", serviceName) + } + + if service.Downto == "" { + t.Errorf("Expected 'downto' for %s when status is 'down'", serviceName) + } + + if service.Comment == "" { + t.Errorf("Expected 'comment' for %s when status is 'down'", serviceName) + } + } + } + + checkService(serviceAvailability.Production.Ingest, "production ingest") + checkService(serviceAvailability.Production.Archive, "production archive") + checkService(serviceAvailability.Qa.Ingest, "qa ingest") + checkService(serviceAvailability.Qa.Archive, "qa archive") +} From 029fbf9a62294edb7303fa82d609c9f3bd130f44 Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:58:21 +0100 Subject: [PATCH 3/9] Remove unused lib --- datasetUtils/checkForServiceAvailability.go | 1 - 1 file changed, 1 deletion(-) diff --git a/datasetUtils/checkForServiceAvailability.go b/datasetUtils/checkForServiceAvailability.go index 443fa0e..3b2eeb5 100644 --- a/datasetUtils/checkForServiceAvailability.go +++ b/datasetUtils/checkForServiceAvailability.go @@ -6,7 +6,6 @@ import ( "log" "net/http" "os" - // "encoding/json" "gopkg.in/yaml.v2" "github.com/fatih/color" ) From e2c2604f1a9924e7a4f9a5c3eff60f912a553ffe Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:10:43 +0100 Subject: [PATCH 4/9] Fix a test --- datasetUtils/checkForServiceAvailability_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/datasetUtils/checkForServiceAvailability_test.go b/datasetUtils/checkForServiceAvailability_test.go index 9626b47..996ecdb 100644 --- a/datasetUtils/checkForServiceAvailability_test.go +++ b/datasetUtils/checkForServiceAvailability_test.go @@ -91,11 +91,7 @@ qa: } checkService := func(service Availability, serviceName string) { - if service.Status != "on" && service.Status != "down" { - t.Errorf("Expected status 'on' or 'down' for %s, got %q", serviceName, service.Status) - } - - if service.Status == "down" { + if service.Status != "on" { if service.Downfrom == "" { t.Errorf("Expected 'downfrom' for %s when status is 'down'", serviceName) } From 0cb747269b6a1477a09959d9f64fbb0223d80934 Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:29:32 +0100 Subject: [PATCH 5/9] Fix the GitHubMainLocation address --- datasetUtils/checkForServiceAvailability.go | 46 ++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/datasetUtils/checkForServiceAvailability.go b/datasetUtils/checkForServiceAvailability.go index 3b2eeb5..3a181f5 100644 --- a/datasetUtils/checkForServiceAvailability.go +++ b/datasetUtils/checkForServiceAvailability.go @@ -26,7 +26,7 @@ type ServiceAvailability struct { Qa OverallAvailability } -var GitHubMainLocation = "https://github.com/paulscherrerinstitute/scicat-cli/blob/main" +var GitHubMainLocation = "https://raw.githubusercontent.com/paulscherrerinstitute/scicat-cli/main" // CheckForServiceAvailability checks the availability of the dataset ingestor service. // It fetches a YAML file from GitHubMainLocation, parses it, and logs the service availability status. @@ -102,29 +102,29 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch } func readYAMLFile(client *http.Client) ([]byte, error) { - // Construct the URL of the service availability YAML file - yamlURL := fmt.Sprintf("%s/cmd/datasetIngestor/datasetIngestorServiceAvailability.yml", GitHubMainLocation) - - // Send a GET request to fetch the service availability YAML file - resp, err := client.Get(yamlURL) - if err != nil { - fmt.Println("No Information about Service Availability") - return nil, fmt.Errorf("failed to fetch the service availability YAML file: %w", err) - } - defer resp.Body.Close() + // Construct the URL of the service availability YAML file + yamlURL := fmt.Sprintf("%s/cmd/datasetIngestor/datasetIngestorServiceAvailability.yml", GitHubMainLocation) + + // Send a GET request to fetch the service availability YAML file + resp, err := client.Get(yamlURL) + if err != nil { + fmt.Println("No Information about Service Availability") + return nil, fmt.Errorf("failed to fetch the service availability YAML file: %w", err) + } + defer resp.Body.Close() - // If the HTTP status code is not 200 (OK), log a message and return - if resp.StatusCode != 200 { - log.Println("No Information about Service Availability") - log.Printf("Error: Got %s fetching %s\n", resp.Status, yamlURL) - return nil, fmt.Errorf("got %s fetching %s", resp.Status, yamlURL) - } + // If the HTTP status code is not 200 (OK), log a message and return + if resp.StatusCode != 200 { + log.Println("No Information about Service Availability") + log.Printf("Error: Got %s fetching %s\n", resp.Status, yamlURL) + return nil, fmt.Errorf("got %s fetching %s", resp.Status, yamlURL) + } - // Read the entire body of the response (the YAML file) - yamlFile, err := io.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("Can not read service availability file for this application") - } + // Read the entire body of the response (the YAML file) + yamlFile, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("Can not read service availability file for this application") + } - return yamlFile, nil + return yamlFile, nil } From d7ff603e5d4b3deda3dc0b2a36ac3317e81035dd Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:40:27 +0100 Subject: [PATCH 6/9] Refactor CheckForServiceAvailability --- datasetUtils/checkForServiceAvailability.go | 74 +++++++++++++-------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/datasetUtils/checkForServiceAvailability.go b/datasetUtils/checkForServiceAvailability.go index 3a181f5..3a519ef 100644 --- a/datasetUtils/checkForServiceAvailability.go +++ b/datasetUtils/checkForServiceAvailability.go @@ -31,39 +31,56 @@ var GitHubMainLocation = "https://raw.githubusercontent.com/paulscherrerinstitut // CheckForServiceAvailability checks the availability of the dataset ingestor service. // It fetches a YAML file from GitHubMainLocation, parses it, and logs the service availability status. func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarchiveFlag bool) { - yamlFile, err := readYAMLFile(client) + s, err := getServiceAvailability(client) if err != nil { - log.Printf("Failed to read service availability YAML file: %v", err) - return + log.Printf("Failed to get service availability: %v", err) + return } + + status, env := determineStatusAndEnv(s, testenvFlag) + + logPlannedDowntime(status, env) + + handleServiceUnavailability(status, env, autoarchiveFlag) +} - // Unmarshal the YAML file into a ServiceAvailability struct +func getServiceAvailability(client *http.Client) (ServiceAvailability, error) { + yamlFile, err := readYAMLFile(client) + if err != nil { + return ServiceAvailability{}, err + } + s := ServiceAvailability{} err = yaml.Unmarshal(yamlFile, &s) if err != nil { - log.Fatalf("Unmarshal of availabilty file failed: %v\n%s", err, yamlFile) + return ServiceAvailability{}, fmt.Errorf("Unmarshal of availabilty file failed: %v\n%s", err, yamlFile) } + + return s, nil +} - // Determine the service status and environment based on the testenvFlag - var status OverallAvailability - var env string - // define default value - status = OverallAvailability{Availability{"on", "", "", ""}, Availability{"on", "", "", ""}} +func determineStatusAndEnv(s ServiceAvailability, testenvFlag bool) (OverallAvailability, string) { + status := OverallAvailability{Availability{"on", "", "", ""}, Availability{"on", "", "", ""}} + env := "production" + if testenvFlag { if (OverallAvailability{}) != s.Qa { status = s.Qa } env = "test" - } else { + } else { if (OverallAvailability{}) != s.Production { - status = s.Production - } - env = "production" + status = s.Production + } } + + return status, env +} +func logPlannedDowntime(status OverallAvailability, env string) { // Reset the terminal color after the function returns defer color.Unset() - + // Log the planned downtime for the ingest and archive services, if any if status.Ingest.Downfrom != "" { color.Set(color.FgYellow) @@ -81,26 +98,29 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch color.Set(color.FgYellow) fmt.Printf("It is scheduled to last until %v\n", status.Archive.Downto) } +} +func handleServiceUnavailability(status OverallAvailability, env string, autoarchiveFlag bool) { // If the ingest service is not available, log a message and terminate the program if status.Ingest.Status != "on" { - color.Set(color.FgRed) - log.Printf("The %s data catalog is currently not available for ingesting new datasets\n", env) - log.Printf("Planned downtime until %v. Reason: %s\n", status.Ingest.Downto, status.Ingest.Comment) - color.Unset() + logServiceUnavailability("ingest", env, status.Ingest) os.Exit(1) } - + // If the archive service is not available and autoarchiveFlag is set, log a message and terminate the program if autoarchiveFlag && status.Archive.Status != "on" { - color.Set(color.FgRed) - log.Printf("The %s data catalog is currently not available for archiving new datasets\n", env) - log.Printf("Planned downtime until %v. Reason: %s\n", status.Archive.Downto, status.Archive.Comment) - color.Unset() + logServiceUnavailability("archive", env, status.Archive) os.Exit(1) } } +func logServiceUnavailability(serviceName string, env string, availability Availability) { + color.Set(color.FgRed) + log.Printf("The %s data catalog is currently not available for %sing new datasets\n", env, serviceName) + log.Printf("Planned downtime until %v. Reason: %s\n", availability.Downto, availability.Comment) + color.Unset() +} + func readYAMLFile(client *http.Client) ([]byte, error) { // Construct the URL of the service availability YAML file yamlURL := fmt.Sprintf("%s/cmd/datasetIngestor/datasetIngestorServiceAvailability.yml", GitHubMainLocation) @@ -112,19 +132,19 @@ func readYAMLFile(client *http.Client) ([]byte, error) { return nil, fmt.Errorf("failed to fetch the service availability YAML file: %w", err) } defer resp.Body.Close() - + // If the HTTP status code is not 200 (OK), log a message and return if resp.StatusCode != 200 { log.Println("No Information about Service Availability") log.Printf("Error: Got %s fetching %s\n", resp.Status, yamlURL) return nil, fmt.Errorf("got %s fetching %s", resp.Status, yamlURL) } - + // Read the entire body of the response (the YAML file) yamlFile, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("Can not read service availability file for this application") } - + return yamlFile, nil } From b19303750218c1a3d660048f4afe844c0647ff45 Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:47:17 +0100 Subject: [PATCH 7/9] Write a test for logServiceUnavailability --- .../checkForServiceAvailability_test.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/datasetUtils/checkForServiceAvailability_test.go b/datasetUtils/checkForServiceAvailability_test.go index 996ecdb..3b3b673 100644 --- a/datasetUtils/checkForServiceAvailability_test.go +++ b/datasetUtils/checkForServiceAvailability_test.go @@ -7,6 +7,9 @@ import ( "testing" "bytes" "gopkg.in/yaml.v2" + "log" + "os" + "strings" ) func TestReadYAMLFile(t *testing.T) { @@ -111,3 +114,33 @@ qa: checkService(serviceAvailability.Qa.Ingest, "qa ingest") checkService(serviceAvailability.Qa.Archive, "qa archive") } + +func TestLogServiceUnavailability(t *testing.T) { + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + serviceName := "ingest" + env := "test" + availability := Availability{ + Status: "off", + Downfrom: "2022-01-01T00:00:00Z", + Downto: "2022-01-02T00:00:00Z", + Comment: "Maintenance", + } + + logServiceUnavailability(serviceName, env, availability) + + want := "The test data catalog is currently not available for ingesting new datasets" + if got := buf.String(); !strings.Contains(got, want) { + t.Errorf("logServiceUnavailability() = %q, want %q", got, want) + } + + want = "Planned downtime until 2022-01-02T00:00:00Z. Reason: Maintenance" + if got := buf.String(); !strings.Contains(got, want) { + t.Errorf("logServiceUnavailability() = %q, want %q", got, want) + } +} + From 7968fe91b4760645c9411710c51b21a9925cf4f1 Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:52:41 +0100 Subject: [PATCH 8/9] Refactor handleServiceUnavailability to use Error instead of os.Exit(1) Add a unit test too --- datasetUtils/checkForServiceAvailability.go | 18 +++++++---- .../checkForServiceAvailability_test.go | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/datasetUtils/checkForServiceAvailability.go b/datasetUtils/checkForServiceAvailability.go index 3a519ef..a551e75 100644 --- a/datasetUtils/checkForServiceAvailability.go +++ b/datasetUtils/checkForServiceAvailability.go @@ -41,7 +41,11 @@ func CheckForServiceAvailability(client *http.Client, testenvFlag bool, autoarch logPlannedDowntime(status, env) - handleServiceUnavailability(status, env, autoarchiveFlag) + err = handleServiceUnavailability(status, env, autoarchiveFlag) + if err != nil { + log.Printf("Error: %v", err) + os.Exit(1) + } } func getServiceAvailability(client *http.Client) (ServiceAvailability, error) { @@ -100,18 +104,20 @@ func logPlannedDowntime(status OverallAvailability, env string) { } } -func handleServiceUnavailability(status OverallAvailability, env string, autoarchiveFlag bool) { - // If the ingest service is not available, log a message and terminate the program +func handleServiceUnavailability(status OverallAvailability, env string, autoarchiveFlag bool) error { + // If the ingest service is not available, log a message and return an error if status.Ingest.Status != "on" { logServiceUnavailability("ingest", env, status.Ingest) - os.Exit(1) + return fmt.Errorf("ingest service is unavailable") } - // If the archive service is not available and autoarchiveFlag is set, log a message and terminate the program + // If the archive service is not available and autoarchiveFlag is set, log a message and return an error if autoarchiveFlag && status.Archive.Status != "on" { logServiceUnavailability("archive", env, status.Archive) - os.Exit(1) + return fmt.Errorf("archive service is unavailable") } + + return nil } func logServiceUnavailability(serviceName string, env string, availability Availability) { diff --git a/datasetUtils/checkForServiceAvailability_test.go b/datasetUtils/checkForServiceAvailability_test.go index 3b3b673..48c8218 100644 --- a/datasetUtils/checkForServiceAvailability_test.go +++ b/datasetUtils/checkForServiceAvailability_test.go @@ -144,3 +144,33 @@ func TestLogServiceUnavailability(t *testing.T) { } } +func TestHandleServiceUnavailability(t *testing.T) { + tests := []struct { + name string + status OverallAvailability + env string + autoarchiveFlag bool + wantErr bool + }{ + { + name: "ingest service unavailable", + status: OverallAvailability{ + Ingest: Availability{Status: "off"}, + Archive: Availability{Status: "on"}, + }, + env: "test", + autoarchiveFlag: false, + wantErr: true, + }, + // Add more test cases here. + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := handleServiceUnavailability(tt.status, tt.env, tt.autoarchiveFlag) + if (err != nil) != tt.wantErr { + t.Errorf("handleServiceUnavailability() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From af6a571f17765a684379ae0a9fa7586b409ebe9f Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:57:06 +0100 Subject: [PATCH 9/9] Add a test for determineStatusAndEnv --- .../checkForServiceAvailability_test.go | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/datasetUtils/checkForServiceAvailability_test.go b/datasetUtils/checkForServiceAvailability_test.go index 48c8218..ee02769 100644 --- a/datasetUtils/checkForServiceAvailability_test.go +++ b/datasetUtils/checkForServiceAvailability_test.go @@ -10,6 +10,7 @@ import ( "log" "os" "strings" + "reflect" ) func TestReadYAMLFile(t *testing.T) { @@ -174,3 +175,45 @@ func TestHandleServiceUnavailability(t *testing.T) { }) } } + +func TestDetermineStatusAndEnv(t *testing.T) { + tests := []struct { + name string + s ServiceAvailability + testenvFlag bool + wantStatus OverallAvailability + wantEnv string + }{ + { + name: "test environment", + s: ServiceAvailability{ + Qa: OverallAvailability{ + Ingest: Availability{Status: "off"}, + Archive: Availability{Status: "on"}, + }, + Production: OverallAvailability{ + Ingest: Availability{Status: "on"}, + Archive: Availability{Status: "on"}, + }, + }, + testenvFlag: true, + wantStatus: OverallAvailability{ + Ingest: Availability{Status: "off"}, + Archive: Availability{Status: "on"}, + }, + wantEnv: "test", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotStatus, gotEnv := determineStatusAndEnv(tt.s, tt.testenvFlag) + if !reflect.DeepEqual(gotStatus, tt.wantStatus) { + t.Errorf("determineStatusAndEnv() gotStatus = %v, want %v", gotStatus, tt.wantStatus) + } + if gotEnv != tt.wantEnv { + t.Errorf("determineStatusAndEnv() gotEnv = %v, want %v", gotEnv, tt.wantEnv) + } + }) + } +}