-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: unit tests code coverage improvements (#126)
- Loading branch information
1 parent
563d572
commit 5effc30
Showing
4 changed files
with
208 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/nearform/initium-cli/src/services/project" | ||
"github.com/urfave/cli/v2" | ||
"gotest.tools/v3/assert" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
const root = "../../" | ||
|
||
const expectedGoAppDockerTemplate = `FROM golang:1.20.4 as build | ||
WORKDIR /go/src/app | ||
COPY . . | ||
RUN go mod download | ||
RUN go vet -v ./... | ||
RUN go test -v ./... | ||
RUN CGO_ENABLED=0 go build -o /go/bin/app | ||
FROM gcr.io/distroless/static-debian11 | ||
COPY --from=build /go/bin/app / | ||
ENTRYPOINT ["/app"] | ||
` | ||
|
||
const expectedNodeAppDockerTemplate = `FROM node:20.2.0 AS build-env | ||
WORKDIR /app | ||
COPY package*.json tsconfig*.json ./ | ||
RUN npm i | ||
COPY . /app | ||
RUN npm run build --if-present | ||
RUN npm test | ||
FROM gcr.io/distroless/nodejs20-debian11 | ||
COPY --from=build-env /app /app | ||
WORKDIR /app | ||
USER nonroot | ||
CMD ["index.js"] | ||
` | ||
|
||
var projects = map[project.ProjectType]map[string]string{ | ||
project.NodeProject: {"directory": "example", "expectedTemplate": expectedNodeAppDockerTemplate}, | ||
project.GoProject: {"directory": ".", "expectedTemplate": expectedGoAppDockerTemplate}, | ||
} | ||
|
||
func TestShouldRenderDockerTemplate(t *testing.T) { | ||
for projectType, props := range projects { | ||
var buffer bytes.Buffer | ||
|
||
cCtx := cli.Context{} | ||
instance := icli{ | ||
project: project.Project{ | ||
Name: string(projectType), | ||
Directory: path.Join(root, props["directory"]), | ||
Resources: os.DirFS(root), | ||
}, | ||
Writer: &buffer, | ||
} | ||
|
||
err := instance.template(&cCtx) | ||
if err != nil { | ||
t.Fatalf(fmt.Sprintf("Error: %v", err)) | ||
} | ||
|
||
expectedTemplate := props["expectedTemplate"] | ||
template := string(buffer.Bytes()) | ||
|
||
assert.Assert(t, template == expectedTemplate, "Expected %s, got %s", expectedTemplate, template) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package k8s | ||
|
||
import ( | ||
"fmt" | ||
"gotest.tools/v3/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
const rootDirectory = "../../../" | ||
|
||
func TestShouldReturnSingleFileContent(t *testing.T) { | ||
expectedFileContent := `apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: initium-cli-sa` | ||
|
||
bytes, err := getFileContent("service-account", os.DirFS(rootDirectory)) | ||
if err != nil { | ||
t.Fatalf(fmt.Sprintf("Error: %s", err)) | ||
} | ||
fileContent := string(bytes) | ||
|
||
assert.Assert(t, expectedFileContent == fileContent, "Expected %s, got %s", expectedFileContent, fileContent) | ||
} | ||
|
||
func TestShouldReturnServiceAccountManifest(t *testing.T) { | ||
expectedFileContent := `--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: initium-cli | ||
rules: | ||
- apiGroups: | ||
- '' | ||
- serving.knative.dev | ||
- apps | ||
- networking.k8s.io | ||
resources: | ||
- namespaces | ||
- deployments | ||
- replicasets | ||
- ingresses | ||
- services | ||
- secrets | ||
verbs: | ||
- create | ||
- delete | ||
- deletecollection | ||
- get | ||
- list | ||
- patch | ||
- update | ||
- watch | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: initium-cli | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: initium-cli | ||
subjects: | ||
- kind: ServiceAccount | ||
name: initium-cli-sa | ||
namespace: default | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: initium-cli-sa | ||
--- | ||
apiVersion: v1 | ||
kind: Secret | ||
type: kubernetes.io/service-account-token | ||
metadata: | ||
name: initium-cli-token | ||
annotations: | ||
kubernetes.io/service-account.name: "initium-cli-sa" | ||
` | ||
|
||
serviceAccountManifest, err := getServiceAccountManifest(os.DirFS(rootDirectory)) | ||
if err != nil { | ||
t.Fatalf(fmt.Sprintf("Error: %s", err)) | ||
} | ||
|
||
assert.Assert(t, expectedFileContent == serviceAccountManifest, "Expected %s, got %s", expectedFileContent, serviceAccountManifest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package utils | ||
|
||
import ( | ||
"gotest.tools/v3/assert" | ||
"testing" | ||
) | ||
|
||
func TestShouldProperlyEncodeLabel(t *testing.T) { | ||
givenLabels := []string{"feature/test", "feature/test$&", "feature/very-long-name-0123456789-0123456789-0123456789-0123456789"} | ||
expectedLabels := []string{"initium-feature-test", "initium-feature-test--", "initium-feature-very-long-name-0123456789-0123456789-0123456-z"} | ||
|
||
for idx, label := range givenLabels { | ||
encodedLabel := EncodeRFC1123(label) | ||
assert.Check(t, encodedLabel == expectedLabels[idx], "Expected %s, got %s", expectedLabels[idx], encodedLabel) | ||
} | ||
} |