Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test suite for servers API testing #103

Closed
wants to merge 13 commits into from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/oran-o2ims
/vendor
/bin/*
.vscode
.vim*
103 changes: 103 additions & 0 deletions test/api/README.md
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions test/api/alarm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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"
"os"
// "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 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://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/api_versions", bytes.NewBuffer([]byte(requestBody)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider having the ORAN URIs (like /o2ims-infrastructureInventory/api_versions) as constants. Hardcoding them multiple times increases the risk of having typos.

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`, "1.0.0"))
})
})
})
21 changes: 21 additions & 0 deletions test/api/api_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api_test

import (
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = BeforeSuite(func() {
Expect(os.Getenv("TEST_HOST")).NotTo(BeZero(), "Please make sure TEST_HOST is set correctly.")
if os.Getenv("TEST_HOST") == "" {
Skip("API tests were skipped because environment variable 'TEST_HOST' isn't set")
}
})

func TestApi(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Api Suite")
}
41 changes: 41 additions & 0 deletions test/api/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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"
"os"
// "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(`{"serviceUri":"","extensions":{},"oCloudId":"123","globalCloudId":"123","name":"OpenShift O-Cloud","description":"OpenShift O-Cloud"}`)
request, _ := http.NewRequest("GET", "https://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/v1/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"))
})
})
})
54 changes: 54 additions & 0 deletions test/api/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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"
"os"
// "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://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/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`, "1.0.0"))
})
})
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://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/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"))
})
})
})
71 changes: 71 additions & 0 deletions test/api/resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package api_test

import (
"bytes"
"crypto/tls"
"io"
"net/http"
"os"

. "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://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/v1/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://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/v1/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`, "local-cluster"))
})
})
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://"+os.Getenv("TEST_HOST")+"/o2ims-infrastructureInventory/v1/resourcePools/local-cluster/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`, "local-cluster"))
})
})
})