-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests, refactor to fix problems
Signed-off-by: Patrice Ferlet <metal3d@gmail.com>
- Loading branch information
Showing
8 changed files
with
429 additions
and
20 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
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,17 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestBuildCommand(t *testing.T) { | ||
rootCmd := buildRootCmd() | ||
if rootCmd == nil { | ||
t.Errorf("Expected rootCmd to be defined") | ||
} | ||
if rootCmd.Use != "katenary" { | ||
t.Errorf("Expected rootCmd.Use to be katenary, got %s", rootCmd.Use) | ||
} | ||
numCommands := 5 | ||
if len(rootCmd.Commands()) != numCommands { | ||
t.Errorf("Expected %d command, got %d", numCommands, len(rootCmd.Commands())) | ||
} | ||
} |
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,115 @@ | ||
package generator | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
v1 "k8s.io/api/apps/v1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func TestBasicCronJob(t *testing.T) { | ||
compose_file := ` | ||
services: | ||
cron: | ||
image: fedora | ||
labels: | ||
katenary.v3/cronjob: | | ||
image: alpine | ||
command: echo hello | ||
schedule: "*/1 * * * *" | ||
rbac: false | ||
` | ||
tmpDir := setup(compose_file) | ||
defer teardown(tmpDir) | ||
|
||
currentDir, _ := os.Getwd() | ||
os.Chdir(tmpDir) | ||
defer os.Chdir(currentDir) | ||
|
||
output := _compile_test(t, "-s", "templates/cron/cronjob.yaml") | ||
cronJob := batchv1.CronJob{} | ||
if err := yaml.Unmarshal([]byte(output), &cronJob); err != nil { | ||
t.Errorf(unmarshalError, err) | ||
} | ||
if cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image != "alpine:latest" { | ||
t.Errorf("Expected image to be alpine, got %s", cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image) | ||
} | ||
combinedCommand := strings.Join(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Command, " ") | ||
if combinedCommand != "sh -c echo hello" { | ||
t.Errorf("Expected command to be sh -c echo hello, got %s", combinedCommand) | ||
} | ||
if cronJob.Spec.Schedule != "*/1 * * * *" { | ||
t.Errorf("Expected schedule to be */1 * * * *, got %s", cronJob.Spec.Schedule) | ||
} | ||
|
||
// ensure that there are a deployment for the fedora Container | ||
var err error | ||
output, err = helmTemplate(ConvertOptions{ | ||
OutputDir: "./chart", | ||
}, "-s", "templates/cron/deployment.yaml") | ||
if err != nil { | ||
t.Errorf("Error: %s", err) | ||
} | ||
deployment := v1.Deployment{} | ||
if err := yaml.Unmarshal([]byte(output), &deployment); err != nil { | ||
t.Errorf(unmarshalError, err) | ||
} | ||
if deployment.Spec.Template.Spec.Containers[0].Image != "fedora:latest" { | ||
t.Errorf("Expected image to be fedora, got %s", deployment.Spec.Template.Spec.Containers[0].Image) | ||
} | ||
} | ||
|
||
func TestCronJobbWithRBAC(t *testing.T) { | ||
compose_file := ` | ||
services: | ||
cron: | ||
image: fedora | ||
labels: | ||
katenary.v3/cronjob: | | ||
image: alpine | ||
command: echo hello | ||
schedule: "*/1 * * * *" | ||
rbac: true | ||
` | ||
|
||
tmpDir := setup(compose_file) | ||
defer teardown(tmpDir) | ||
|
||
currentDir, _ := os.Getwd() | ||
os.Chdir(tmpDir) | ||
defer os.Chdir(currentDir) | ||
|
||
output := _compile_test(t, "-s", "templates/cron/cronjob.yaml") | ||
cronJob := batchv1.CronJob{} | ||
if err := yaml.Unmarshal([]byte(output), &cronJob); err != nil { | ||
t.Errorf(unmarshalError, err) | ||
} | ||
if cronJob.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName == "" { | ||
t.Errorf("Expected ServiceAccountName to be set") | ||
} | ||
|
||
// find the service account file | ||
output, err := helmTemplate(ConvertOptions{ | ||
OutputDir: "./chart", | ||
}, "-s", "templates/cron/serviceaccount.yaml") | ||
if err != nil { | ||
t.Errorf("Error: %s", err) | ||
} | ||
serviceAccount := corev1.ServiceAccount{} | ||
|
||
if err := yaml.Unmarshal([]byte(output), &serviceAccount); err != nil { | ||
t.Errorf(unmarshalError, err) | ||
} | ||
if serviceAccount.Name == "" { | ||
t.Errorf("Expected ServiceAccountName to be set") | ||
} | ||
|
||
// ensure that the serviceAccount is equal to the cronJob | ||
if serviceAccount.Name != cronJob.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName { | ||
t.Errorf("Expected ServiceAccountName to be %s, got %s", cronJob.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName, serviceAccount.Name) | ||
} | ||
} |
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,76 @@ | ||
package generator | ||
|
||
import ( | ||
_ "embed" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testingKatenaryPrefix = Prefix() | ||
|
||
func TestPrefix(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{ | ||
name: "TestPrefix", | ||
want: "katenary.v3", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Prefix(); got != tt.want { | ||
t.Errorf("Prefix() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_labelName(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Label | ||
}{ | ||
{ | ||
name: "Test_labelName", | ||
args: args{ | ||
name: "main-app", | ||
}, | ||
want: testingKatenaryPrefix + "/main-app", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := labelName(tt.args.name); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("labelName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetLabelHelp(t *testing.T) { | ||
help := GetLabelHelp(false) | ||
if help == "" { | ||
t.Errorf("GetLabelHelp() = %v, want %v", help, "Help") | ||
} | ||
help = GetLabelHelp(true) | ||
if help == "" { | ||
t.Errorf("GetLabelHelp() = %v, want %v", help, "Help") | ||
} | ||
} | ||
|
||
func TestGetLabelHelpFor(t *testing.T) { | ||
help := GetLabelHelpFor("main-app", false) | ||
if help == "" { | ||
t.Errorf("GetLabelHelpFor() = %v, want %v", help, "Help") | ||
} | ||
help = GetLabelHelpFor("main-app", true) | ||
if help == "" { | ||
t.Errorf("GetLabelHelpFor() = %v, want %v", help, "Help") | ||
} | ||
} |
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,8 @@ | ||
package labelstructs | ||
|
||
type CronJob struct { | ||
Image string `yaml:"image,omitempty"` | ||
Command string `yaml:"command"` | ||
Schedule string `yaml:"schedule"` | ||
Rbac bool `yaml:"rbac"` | ||
} |
Oops, something went wrong.