github.com/diggerhq/digger/libs@v0.0.0-20240604170430-9d61cdf01cc5/orchestrator/json_models_test.go (about) 1 package orchestrator 2 3 import ( 4 "fmt" 5 "github.com/stretchr/testify/assert" 6 "reflect" 7 "slices" 8 "testing" 9 ) 10 11 func TestAllFieldsInJobJsonAreAlsoInJob(t *testing.T) { 12 spec := JobJson{} 13 job := Job{} 14 15 specVal := reflect.Indirect(reflect.ValueOf(spec)) 16 nFieldsSpec := specVal.Type().NumField() 17 18 jobVal := reflect.Indirect(reflect.ValueOf(job)) 19 nFieldsJob := jobVal.Type().NumField() 20 jobFileds := make([]string, 0) 21 22 for j := 0; j < nFieldsJob; j++ { 23 jobFileds = append(jobFileds, jobVal.Type().Field(j).Name) 24 } 25 26 fieldsToIgnore := []string{"Commit", "Branch", "JobType", "AwsRoleRegion", "StateRoleName", "CommandRoleName", "BackendHostname", "BackendOrganisationName", "BackendJobToken"} 27 for i := 0; i < nFieldsSpec; i++ { 28 field := specVal.Type().Field(i).Name 29 if slices.Contains(fieldsToIgnore, field) { 30 continue 31 } 32 assert.True(t, slices.Contains(jobFileds, field), 33 "IMPORTANT: Please ensure all fields are correctly passed in serialization") 34 } 35 } 36 37 func TestAllFieldsInJobAreAlsoInJobJson(t *testing.T) { 38 spec := JobJson{} 39 job := Job{} 40 41 jobVal := reflect.Indirect(reflect.ValueOf(job)) 42 nFieldsJob := jobVal.Type().NumField() 43 44 specVal := reflect.Indirect(reflect.ValueOf(spec)) 45 nFieldsSpec := specVal.Type().NumField() 46 specFields := make([]string, 0) 47 for j := 0; j < nFieldsSpec; j++ { 48 specFields = append(specFields, specVal.Type().Field(j).Name) 49 } 50 51 fmt.Printf("%v ::\n", specFields) 52 fieldsToIgnore := []string{"ProjectWorkflow", "StateEnvProvider", "CommandEnvProvider"} 53 for i := 0; i < nFieldsJob; i++ { 54 field := jobVal.Type().Field(i).Name 55 if slices.Contains(fieldsToIgnore, field) { 56 continue 57 } 58 assert.True(t, slices.Contains(specFields, field), 59 "IMPORTANT: Please ensure all fields are correctly passed in serialization") 60 } 61 } 62 63 func TestIsPlanForDiggerPlanJobCorrect(t *testing.T) { 64 j := JobJson{ 65 ProjectName: "project.Name", 66 ProjectDir: "project.Dir", 67 ProjectWorkspace: "workspace", 68 Terragrunt: false, 69 Commands: []string{"run echo 'hello", "digger plan"}, 70 EventName: "issue_comment", 71 } 72 assert.True(t, j.IsPlan()) 73 assert.False(t, j.IsApply()) 74 } 75 76 func TestIsApplyForDiggerApplyJobCorrect(t *testing.T) { 77 j := JobJson{ 78 ProjectName: "project.Name", 79 ProjectDir: "project.Dir", 80 ProjectWorkspace: "workspace", 81 Terragrunt: false, 82 Commands: []string{"digger apply"}, 83 EventName: "issue_comment", 84 } 85 assert.True(t, j.IsApply()) 86 assert.False(t, j.IsPlan()) 87 }