diff --git a/.gitignore b/.gitignore index e0931f46..834f660a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /oran-o2ims /vendor /bin/* +.vscode +.vim* diff --git a/test/api/README.md b/test/api/README.md new file mode 100644 index 00000000..0061357b --- /dev/null +++ b/test/api/README.md @@ -0,0 +1,103 @@ +# ORAN API TEST SUITE +## Introduction + +This test suite needs of a functional Openshift deployment with ACM and ORAN operator, servers must be started too + +```bash +oran-o2ims Active 12d +oran-o2ims-system Active 12d +rhacm Active 12d +``` + +In this case *rhacm* is the ACM namespace, *oran-o2ims-system* is the operator namespace and *oran-o2ims* is where the servers are deployed + +```bash +NAME READY STATUS RESTARTS AGE +deployment-manager-server-66d5b544f4-94p96 1/1 Running 0 12d +metadata-server-64b4597c94-9q4sk 1/1 Running 0 12d +resource-server-646bccdb87-65jwr 1/1 Running 0 12d +``` + +## Preparation + +For running correctly the testsuite we need to set **TEST_HOST** env variable, this variable is the apps URL that we can get using this command + +```bash + oc describe routes -n oran-o2ims | grep Host + Requested Host: o2ims.apps.ocp-mobius-cluster-assisted-0.qe.lab.redhat.com +``` + +Then we can use this +```bash +export TEST_HOST="o2ims.apps.ocp-mobius-cluster-assisted-0.qe.lab.redhat.com" +``` + +Or we can launch the test suite without setting with export this way + +```bash +TEST_HOST="o2ims.apps.ocp-mobius-cluster-assisted-0.qe.lab.redhat.com" go run github.com/onsi/ginkgo/v2/ginkgo -v +``` + +## Execution and results + +To run the test we have some ways to do it, for example, if we have used the export command we can run + +```bash +ginkgo -v +``` +**Note:** probably you get a ginkgo version mismatch, if you want to get rid of the message is better to launch with the repo version like this + +```bash +go run github.com/onsi/ginkgo/v2/ginkgo -v +``` + +This gives a result like this + +```bash +Running Suite: OranO2ims Suite - /home/sdelacru/repos/oran-o2ims +================================================================ +Random Seed: 1717072815 + +Will run 2 of 7 specs +------------------------------ +Metadata Server API testing When getting infrastructure Inventory API version should return OK in the response and json response should match reference json +/home/sdelacru/repos/oran-o2ims/oran_o2ims_suite_test.go:18 + STEP: Executing https petition @ 05/30/24 14:40:17.779 + STEP: Checking OK status response @ 05/30/24 14:40:18.091 + STEP: Checking response JSON is equal to expected JSON @ 05/30/24 14:40:18.092 +• [0.313 seconds] +------------------------------ +Metadata Server API testing When getting infrastructure Inventory description should return OK in the response and json response should match reference json +/home/sdelacru/repos/oran-o2ims/oran_o2ims_suite_test.go:40 + STEP: Executing https petition @ 05/30/24 14:40:18.092 + STEP: Checking OK status response @ 05/30/24 14:40:18.418 + STEP: Checking response JSON is equal to expected JSON @ 05/30/24 14:40:18.418 +• [0.326 seconds] +------------------------------ +SSSSS + +Ran 2 of 7 Specs in 0.640 seconds +SUCCESS! -- 2 Passed | 0 Failed | 0 Pending | 5 Skipped +PASS + +Ginkgo ran 1 suite in 3.296458911s +Test Suite Passed +``` + +## Advanced usage +We can run selected groups of tests, like this way + +```bash +TEST_HOST="o2ims.apps.ocp-hub-0.qe.lab.redhat.com" go run github.com/onsi/ginkgo/v2/ginkgo --focus=Metadata -v +``` +with *focus* parameter we can launch only a group of tests, in this case metadata server only test cases, we can do the same with the other servers, for example *--focus=Resources* will run only resource server tests + + +## TODO + +- [ ] Check running servers in BeforeSuite +- [ ] Add labels to tag the indivual tests so we can run specific tests depending on test phase (integration, development, release...etc) +- [ ] Add more tests in next iteration +- [x] Use BeforeEach for the HTTP client +- [x] Separate servers testcases in different files +- [ ] Add alarm server testcases diff --git a/test/api/alarm_test.go b/test/api/alarm_test.go new file mode 100644 index 00000000..81d13175 --- /dev/null +++ b/test/api/alarm_test.go @@ -0,0 +1,40 @@ +package api_test + +import ( + "bytes" + "crypto/tls" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + . "github.com/openshift-kni/oran-o2ims/internal/testing" + "io" + "net/http" + // "github.com/openshift-kni/oran-o2ims/test/api" +) + +var _ = Describe("Alarm Server test API", func() { + var client *http.Client + + BeforeEach(func() { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client = &http.Client{Transport: tr} + }) + Context("When getting alarm Inventory", func() { + It("should return OK in the response and json response should match reference json", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://"+ testHost + alarUrl + "alarms", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing https petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking response JSON matches condition") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`version`, version)) + }) + }) +}) diff --git a/test/api/api_suite_test.go b/test/api/api_suite_test.go new file mode 100644 index 00000000..207ee99a --- /dev/null +++ b/test/api/api_suite_test.go @@ -0,0 +1,27 @@ +package api_test + +import ( + "os" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) +var testHost string +const version = "1.0.0" +const resPool = "local-cluster" +const metaUrl = "/o2ims-infrastructureInventory/" +const resUrl = "/o2ims-infrastructureInventory/v1/" +const alarUrl = "/o2ims-infrastructureMonitoring/v1/" +var _ = BeforeSuite(func() { + testHost := os.Getenv("TEST_HOST") + Expect(testHost).NotTo(BeZero(), "Please make sure TEST_HOST is correctly set") + if testHost == "" { + Skip("API tests were skipped because environment variable 'TEST_HOST' isn't set") + } +}) + +func TestApi(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Api Suite") +} diff --git a/test/api/deployment_test.go b/test/api/deployment_test.go new file mode 100644 index 00000000..55b9fb50 --- /dev/null +++ b/test/api/deployment_test.go @@ -0,0 +1,42 @@ +package api_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "bytes" + "crypto/tls" + . "github.com/openshift-kni/oran-o2ims/internal/testing" + "io" + "net/http" + // "github.com/openshift-kni/oran-o2ims/test/api" +) + +var _ = Describe("Deployment Manager Server API testing", func() { + var client *http.Client + + BeforeEach(func() { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client = &http.Client{Transport: tr} + }) + + Context("When getting Deployment managers description", func() { + It("should return OK in the response and json response should match json", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://" + testHost + resUrl + "deploymentManagers", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing http petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking JSON match") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`.name`, "OpenShift O-Cloud")) + }) + }) +}) diff --git a/test/api/metadata_test.go b/test/api/metadata_test.go new file mode 100644 index 00000000..bf2a29c1 --- /dev/null +++ b/test/api/metadata_test.go @@ -0,0 +1,56 @@ +package api_test + +import ( + "bytes" + "crypto/tls" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + . "github.com/openshift-kni/oran-o2ims/internal/testing" + "io" + "net/http" + // "github.com/openshift-kni/oran-o2ims/test/api" +) + +var _ = Describe("Metadata Server API testing", func() { + var client *http.Client + BeforeEach(func() { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client = &http.Client{Transport: tr} + }) + Context("When getting infrastructure Inventory API version", func() { + It("should return OK in the response and json response should match reference json", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://"+ testHost + metaUrl + "api_versions", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing https petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking response JSON matches condition") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`version`, version)) + }) + }) + Context("When getting infrastructure Inventory description", func() { + It("should return OK in the response and json response should match reference json", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://" + testHost + metaUrl + "v1", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing https petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking response JSON matches condition") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`.name`, "OpenShift O-Cloud")) + }) + }) +}) diff --git a/test/api/resources_test.go b/test/api/resources_test.go new file mode 100644 index 00000000..3f5810d6 --- /dev/null +++ b/test/api/resources_test.go @@ -0,0 +1,76 @@ +package api_test + +import ( + "bytes" + "crypto/tls" + "io" + "net/http" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + // "github.com/openshift-kni/oran-o2ims/test/api" + . "github.com/openshift-kni/oran-o2ims/internal/testing" +) + +var _ = Describe("Resources Server API testing", func() { + var client *http.Client + + BeforeEach(func() { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client = &http.Client{Transport: tr} + }) + + Context("When getting Resource Type list", func() { + It("should return OK in the response and json response should match condition", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://" + testHost + resUrl + "resourceTypes", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing https petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking response JSON matches condition") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`.name`, "OpenShift O-Cloud")) + }) + }) + Context("When getting Resource Pools", func() { + It("should return OK in the response and json response should match reference json", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://" + testHost + resUrl + "resourcePools", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing https petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking response JSON is equal to expected JSON") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`.name`, resPool)) + }) + }) + Context("When getting Resource List from a defined pool", func() { + It("should return OK in the response and json response should match reference json", func() { + requestBody := []byte(``) + request, _ := http.NewRequest("GET", + "https://" + testHost + resUrl + "resourcePools/" + resPool + "/resources", + bytes.NewBuffer([]byte(requestBody))) + request.Header.Set("Content-Type", "application/json") + By("Executing https petition") + response, err := client.Do(request) + Expect(err).NotTo(HaveOccurred()) + By("Checking OK status response") + Expect(response.StatusCode).To(Equal(http.StatusOK)) + By("Checking response JSON matches condition") + responseBody, _ := io.ReadAll(response.Body) + Expect(responseBody).To(MatchJQ(`.resourcePoolID`, resPool)) + }) + }) +})