Skip to content

Commit

Permalink
chore: code coverage improvements (knative.go)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaremba-tomasz committed Oct 10, 2023
1 parent 6ac6317 commit 6d98194
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package k8s

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/nearform/initium-cli/src/utils/defaults"
"gotest.tools/v3/assert"
"io"
"knative.dev/pkg/apis"
v1 "knative.dev/serving/pkg/apis/serving/v1"
"net/http"
"net/url"
"os"
"path"
"testing"
Expand All @@ -20,6 +28,38 @@ var endpoint = "https://127.0.0.1:6443"

var token = "ZXlKaGJHY2lPaUpTVXpJMU5pSXNJbXRwWkNJNklqWllaRFEwT0ZwUWVsRlJRVkp0TUMxa04wWTRNV3N6UzFwVmRXOVhSMDlvYzA5MWFESXdPVWhwVkhjaWZRLmV5SnBjM01pT2lKcmRXSmxjbTVsZEdWekwzTmxjblpwWTJWaFkyTnZkVzUwSWl3aWEzVmlaWEp1WlhSbGN5NXBieTl6WlhKMmFXTmxZV05qYjNWdWRDOXVZVzFsYzNCaFkyVWlPaUpyZFdKbExYTjVjM1JsYlNJc0ltdDFZbVZ5Ym1WMFpYTXVhVzh2YzJWeWRtbGpaV0ZqWTI5MWJuUXZjMlZqY21WMExtNWhiV1VpT2lKdmEyVXRhM1ZpWldOdmJtWnBaeTF6WVMxMGIydGxiaUlzSW10MVltVnlibVYwWlhNdWFXOHZjMlZ5ZG1salpXRmpZMjkxYm5RdmMyVnlkbWxqWlMxaFkyTnZkVzUwTG01aGJXVWlPaUowWlhOMExYTmhJaXdpYTNWaVpYSnVaWFJsY3k1cGJ5OXpaWEoyYVdObFlXTmpiM1Z1ZEM5elpYSjJhV05sTFdGalkyOTFiblF1ZFdsa0lqb2lNelV4TkdJME9XVXRaVE15TWkwMFpHTTBMVGhsTUdVdE1qSTROemxsWm1Ga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/PcZBRWUSQo3wRHW6b8upS7TTHF3ScTWjAyIHPC
kMhPQ4zVtC8Zc5gDLKMCAwEAAaNZMFcwDgYDVR0PAQH/BAQDAgKkMA8GA1UdEwEB
/wQFMAMBAf8wHQYDVR0OBBYEFLq5/eX3KD4kKFZnFmRiauBTfRppMBUGA1UdEQQO
MAyCCmt1YmVybmV0ZXMwDQYJKoZIhvcNAQELBQADggEBALqcDaumB0+7EEsazzK5
BqnOaNW5Cq255rcz+aC+elvECuFRQ4nliG3jTADfbZ1dMZiF95fzPdkbOKDIMy4n
NkgexnqrrxcP6MmMhZeUb5o5UG9RUVg2CBd17menE2ogMmq6IFyR6l1qoecQtB3T
EQb0s5BWHqJ7HgIEgaURKCdZEtgjdXQZxvx5U7JqY7HDqeMpQlad3kYsKmZcsAGM
f1kAZcMbBJuBbCNuTJoOYeLIiBH73JjNSn4k27JsrCuAO33InyWKNDNB/WN/ghV1
tn+2QMzMAynSx2vmpWqDiomiHYZoLrv9yQhIfGoT9CwEMQn5ciOUzDvT214W/Ryp
+Mw=
-----END CERTIFICATE-----
`

type transportFunc func(*http.Request) (*http.Response, error)

func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return tf(req)
}

type knativeApiRequest struct {
httpMethod string
url string
}

func TestConfig(t *testing.T) {

decodedCert, err := base64.StdEncoding.DecodeString(caCrt)
Expand Down Expand Up @@ -81,3 +121,97 @@ func TestLoadManifest(t *testing.T) {
assert.Assert(t, annotations[UpdateShaAnnotationName] == commitSha, "Expected %s SHA, got %s", commitSha, annotations[UpdateShaAnnotationName])
}

func TestApply(t *testing.T) {
config, err := Config(endpoint, token, []byte(certificate))
if err != nil {
t.Fatalf(fmt.Sprintf("Error: %s", err))
}

var apiRequests []knativeApiRequest
getServiceRequestCount := 0
handler := func(request *http.Request) (*http.Response, error) {
apiRequests = append(apiRequests, knativeApiRequest{
httpMethod: request.Method,
url: request.URL.Path,
})

header := http.Header{}
header.Set("Content-Type", "application/json")

// simulate new service creation
if request.Method == http.MethodGet && request.URL.Path == "/apis/serving.knative.dev/v1/namespaces/default/services/initium-cli" {
if getServiceRequestCount == 0 {
getServiceRequestCount += 1
return &http.Response{
StatusCode: 404,
Body: io.NopCloser(bytes.NewReader([]byte("{}"))),
Header: header,
}, nil
} else {
statusUrl, handlerErr := url.Parse("http://initium-nodejs-demo-app.initium-chore-another-pr.example.com")
if handlerErr != nil {
t.Fatalf(fmt.Sprintf("Error: %s", err))
}
service := v1.Service{
Status: v1.ServiceStatus{
RouteStatusFields: v1.RouteStatusFields{
URL: (*apis.URL)(statusUrl),
},
},
}
marshaledJson, handlerErr := json.Marshal(service)
if handlerErr != nil {
t.Fatalf(fmt.Sprintf("Error: %s", err))
}

return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader(marshaledJson)),
Header: header,
}, nil
}
} else {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte("{}"))),
Header: header,
}, nil
}
}

config.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return transportFunc(handler)
})

proj := project.Project{
Name: "initium-cli",
Directory: path.Join("../../../", "."),
Resources: os.DirFS("../../../"),
}

dockerImage := docker.DockerImage{
Registry: "example.org",
Directory: defaults.ProjectDirectory,
Name: "test",
Tag: "v1.1.0",
}

err = Apply("default", "6ac63179", config, &proj, dockerImage, defaults.EnvVarFile)
if err != nil {
t.Fatalf(fmt.Sprintf("Error: %s", err))
}

assert.Check(t, len(apiRequests) == 4, "Expected 4 requests to be sent to Knative API")

assert.Assert(t, apiRequests[0].httpMethod == http.MethodPost, "Expected POST method to be called")
assert.Assert(t, apiRequests[0].url == "/api/v1/namespaces", "Expected URL suffix to be /api/v1/namespaces")

assert.Assert(t, apiRequests[1].httpMethod == http.MethodGet, "Expected GET method to be called")
assert.Assert(t, apiRequests[1].url == "/apis/serving.knative.dev/v1/namespaces/default/services/initium-cli", "Expected URL suffix to be /apis/serving.knative.dev/v1/namespaces/default/services/initium-cli")

assert.Assert(t, apiRequests[2].httpMethod == http.MethodPost, "Expected POST method to be called")
assert.Assert(t, apiRequests[2].url == "/apis/serving.knative.dev/v1/namespaces/default/services", "Expected URL suffix to be /apis/serving.knative.dev/v1/namespaces/default/services")

assert.Assert(t, apiRequests[3].httpMethod == http.MethodGet, "Expected GET method to be called")
assert.Assert(t, apiRequests[3].url == "/apis/serving.knative.dev/v1/namespaces/default/services/initium-cli", "Expected URL suffix to be /apis/serving.knative.dev/v1/namespaces/default/services/initium-cli")
}

0 comments on commit 6d98194

Please sign in to comment.