Skip to content

Commit

Permalink
Merge pull request #2 from bosesuneha/sunehabose/generate-workflow-tests
Browse files Browse the repository at this point in the history
Sunehabose/generate workflow tests
  • Loading branch information
aamgayle authored Apr 7, 2023
2 parents f940cb3 + 48dd93e commit e69f887
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
45 changes: 43 additions & 2 deletions pkg/workflows/workflows_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package workflows

import (
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -44,24 +45,55 @@ func TestCreateWorkflows(t *testing.T) {
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))
os.RemoveAll("manifests")
os.RemoveAll(".github")
//test for missing deployment file path
assert.NotNil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))

//test for invalid deployType
deployType = "testInvalidDeployType"
assert.NotNil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))

}

func TestUpdateProductionDeployments(t *testing.T) {
flagValuesMap := map[string]string{"AZURECONTAINERREGISTRY": "testRegistry", "CONTAINERNAME": "testContainer"}
testTemplateWriter := &writers.LocalFSWriter{}
//test for missing deploy type
assert.Nil(t, updateProductionDeployments("", ".", flagValuesMap, testTemplateWriter))

//test for missing helm deployment file
assert.NotNil(t, setHelmContainerImage("", "testImage", testTemplateWriter))

//test for invalid helm deployment file
tempFile, err := ioutil.TempFile("", "*.yaml")
assert.Nil(t, err)
defer os.Remove(tempFile.Name())
yamlData := []byte(`not a valid yaml`)
_, err = tempFile.Write(yamlData)
assert.Nil(t, err)
err = tempFile.Close()
assert.Nil(t, err)
assert.NotNil(t, setHelmContainerImage(tempFile.Name(), "testImage", testTemplateWriter))

//test for valid helm deployment file
helmFileName, _ := createTempManifest("../../test/templates/helm_prod_values.yaml")
deploymentFileName, _ := createTempManifest("../../test/templates/deployment.yaml")
defer os.Remove(helmFileName)
defer os.Remove(deploymentFileName)

assert.Nil(t, setHelmContainerImage(helmFileName, "testImage", testTemplateWriter))

helmDeploy := &HelmProductionYaml{}
assert.Nil(t, helmDeploy.LoadFromFile(helmFileName))
assert.Equal(t, "testImage", helmDeploy.Image.Repository)

//test for missing deployment file
assert.NotNil(t, setDeploymentContainerImage("", "testImage"))

//test for invalid deployment file
assert.NotNil(t, setDeploymentContainerImage(tempFile.Name(), "testImage"))

//test for valid deployment file
deploymentFileName, _ := createTempManifest("../../test/templates/deployment.yaml")
defer os.Remove(deploymentFileName)

assert.Nil(t, setDeploymentContainerImage(deploymentFileName, "testImage"))
decode := scheme.Codecs.UniversalDeserializer().Decode
file, err := ioutil.ReadFile(deploymentFileName)
Expand All @@ -73,6 +105,15 @@ func TestUpdateProductionDeployments(t *testing.T) {
deploy, ok := k8sObj.(*appsv1.Deployment)
assert.True(t, ok)
assert.Equal(t, "testImage", deploy.Spec.Template.Spec.Containers[0].Image)

//test for invalid k8sObj
invalidDeploymentFile, _ := createTempManifest("../../test/templates/invalid_deployment.yaml")
assert.Equal(t, errors.New("could not decode kubernetes deployment"), setDeploymentContainerImage(invalidDeploymentFile, "testImage"))

//test for unsupported number of containers in the deployment spec
invalidDeploymentFile, _ = createTempManifest("../../test/templates/unsupported_no_of_containers.yaml")
defer os.Remove(invalidDeploymentFile)
assert.Equal(t, errors.New("unsupported number of containers defined in the deployment spec"), setDeploymentContainerImage(invalidDeploymentFile, "testImage"))
}

func TestLoadConfig(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions test/templates/invalid_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
kind: Pod
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app
ports:
- containerPort: 8000
25 changes: 25 additions & 0 deletions test/templates/unsupported_no_of_containers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app
ports:
- containerPort: 8000
- name: my-app-2
image: my-app-2
ports:
- containerPort: 8080

0 comments on commit e69f887

Please sign in to comment.