|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ory/viper" |
| 9 | + "k8s.io/utils/pointer" |
| 10 | + fn "knative.dev/kn-plugin-func" |
| 11 | + "knative.dev/kn-plugin-func/mock" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_runDeploy(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + gitURL string |
| 18 | + gitBranch string |
| 19 | + gitDir string |
| 20 | + buildType string |
| 21 | + funcFile string |
| 22 | + expectFileURL *string |
| 23 | + expectFileBranch *string |
| 24 | + expectFileContextDir *string |
| 25 | + expectCallURL *string |
| 26 | + expectCallBranch *string |
| 27 | + expectCallContextDir *string |
| 28 | + errString string |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "Git arguments don't get saved to func.yaml but are used in the pipeline invocation", |
| 32 | + gitURL: "git@github.com:knative-sandbox/kn-plugin-func.git", |
| 33 | + gitBranch: "main", |
| 34 | + gitDir: "func", |
| 35 | + buildType: fn.BuildTypeGit, |
| 36 | + funcFile: `name: test-func |
| 37 | +runtime: go |
| 38 | +created: 2009-11-10 23:00:00`, |
| 39 | + expectCallURL: pointer.StringPtr("git@github.com:knative-sandbox/kn-plugin-func.git"), |
| 40 | + expectCallBranch: pointer.StringPtr("main"), |
| 41 | + expectCallContextDir: pointer.StringPtr("func"), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Git url gets split when in the format url#branch", |
| 45 | + gitURL: "git@github.com:knative-sandbox/kn-plugin-func.git#main", |
| 46 | + gitDir: "func", |
| 47 | + buildType: fn.BuildTypeGit, |
| 48 | + funcFile: `name: test-func |
| 49 | +runtime: go |
| 50 | +created: 2009-11-10 23:00:00`, |
| 51 | + expectCallURL: pointer.StringPtr("git@github.com:knative-sandbox/kn-plugin-func.git"), |
| 52 | + expectCallBranch: pointer.StringPtr("main"), |
| 53 | + expectCallContextDir: pointer.StringPtr("func"), |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Git arguments override func.yaml but don't get saved", |
| 57 | + gitURL: "git@github.com:knative-sandbox/kn-plugin-func.git", |
| 58 | + gitBranch: "main", |
| 59 | + gitDir: "func", |
| 60 | + funcFile: `name: test-func |
| 61 | +runtime: go |
| 62 | +created: 2009-11-10 23:00:00 |
| 63 | +build: git |
| 64 | +git: |
| 65 | + url: git@github.com:my-repo/my-function.git |
| 66 | + revision: master |
| 67 | + contextDir: pwd`, |
| 68 | + expectCallURL: pointer.StringPtr("git@github.com:knative-sandbox/kn-plugin-func.git"), |
| 69 | + expectCallBranch: pointer.StringPtr("main"), |
| 70 | + expectCallContextDir: pointer.StringPtr("func"), |
| 71 | + expectFileURL: pointer.StringPtr("git@github.com:my-repo/my-function.git"), |
| 72 | + expectFileBranch: pointer.StringPtr("master"), |
| 73 | + expectFileContextDir: pointer.StringPtr("pwd"), |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Git properties work without arguments", |
| 77 | + funcFile: `name: test-func |
| 78 | +runtime: go |
| 79 | +created: 2009-11-10 23:00:00 |
| 80 | +build: git |
| 81 | +git: |
| 82 | + url: git@github.com:my-repo/my-function.git |
| 83 | + revision: master |
| 84 | + contextDir: pwd`, |
| 85 | + expectFileURL: pointer.StringPtr("git@github.com:my-repo/my-function.git"), |
| 86 | + expectFileBranch: pointer.StringPtr("master"), |
| 87 | + expectFileContextDir: pointer.StringPtr("pwd"), |
| 88 | + expectCallURL: pointer.StringPtr("git@github.com:my-repo/my-function.git"), |
| 89 | + expectCallBranch: pointer.StringPtr("master"), |
| 90 | + expectCallContextDir: pointer.StringPtr("pwd"), |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "check error when providing git flags with buildType local", |
| 94 | + gitURL: "git@github.com:my-repo/my-function.git", |
| 95 | + buildType: "local", |
| 96 | + funcFile: `name: test-func |
| 97 | +runtime: go |
| 98 | +created: 2009-11-10 23:00:00`, |
| 99 | + errString: "remote git arguments require the --build=git flag", |
| 100 | + }, |
| 101 | + } |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + var captureFn fn.Function |
| 105 | + pipeline := &mock.PipelinesProvider{ |
| 106 | + RunFn: func(f fn.Function) error { |
| 107 | + captureFn = f |
| 108 | + return nil |
| 109 | + }, |
| 110 | + } |
| 111 | + deployer := mock.NewDeployer() |
| 112 | + defer fromTempDir(t)() |
| 113 | + cmd := NewDeployCmd(func(opts ClientOptions) *fn.Client { |
| 114 | + return fn.New( |
| 115 | + fn.WithPipelinesProvider(pipeline), |
| 116 | + fn.WithDeployer(deployer), |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + viper.SetDefault("git-url", tt.gitURL) |
| 121 | + viper.SetDefault("git-branch", tt.gitBranch) |
| 122 | + viper.SetDefault("git-dir", tt.gitDir) |
| 123 | + viper.SetDefault("build", tt.buildType) |
| 124 | + viper.SetDefault("registry", "docker.io/tigerteam") |
| 125 | + |
| 126 | + // set test case's func.yaml |
| 127 | + if err := os.WriteFile("func.yaml", []byte(tt.funcFile), os.ModePerm); err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + ctx := context.TODO() |
| 132 | + |
| 133 | + _, err := cmd.ExecuteContextC(ctx) |
| 134 | + if err != nil { |
| 135 | + if tt.errString == "" { |
| 136 | + t.Fatalf("Problem executing command: %v", err) |
| 137 | + } else if err := err.Error(); tt.errString != err { |
| 138 | + t.Fatalf("Error expected to be %v but was %v", tt.errString, err) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + fileFunction, err := fn.NewFunction(".") |
| 143 | + |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("problem creating function: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + { |
| 149 | + if fileURL, expectedURL := pointer.StringPtrDerefOr(fileFunction.Git.URL, ""), pointer.StringPtrDerefOr(tt.expectFileURL, ""); fileURL != expectedURL { |
| 150 | + t.Fatalf("file Git URL expected to be (%v) but was (%v)", expectedURL, fileURL) |
| 151 | + } |
| 152 | + if fileBranch, expectedBranch := pointer.StringPtrDerefOr(fileFunction.Git.Revision, ""), pointer.StringPtrDerefOr(tt.expectFileBranch, ""); fileBranch != expectedBranch { |
| 153 | + t.Fatalf("file Git branch expected to be (%v) but was (%v)", expectedBranch, fileBranch) |
| 154 | + } |
| 155 | + if fileDir, expectedDir := pointer.StringPtrDerefOr(fileFunction.Git.ContextDir, ""), pointer.StringPtrDerefOr(tt.expectFileContextDir, ""); fileDir != expectedDir { |
| 156 | + t.Fatalf("file Git contextDir expected to be (%v) but was (%v)", expectedDir, fileDir) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + { |
| 161 | + if caputureURL, expectedURL := pointer.StringPtrDerefOr(captureFn.Git.URL, ""), pointer.StringPtrDerefOr(tt.expectCallURL, ""); caputureURL != expectedURL { |
| 162 | + t.Fatalf("call Git URL expected to be (%v) but was (%v)", expectedURL, caputureURL) |
| 163 | + } |
| 164 | + if captureBranch, expectedBranch := pointer.StringPtrDerefOr(captureFn.Git.Revision, ""), pointer.StringPtrDerefOr(tt.expectCallBranch, ""); captureBranch != expectedBranch { |
| 165 | + t.Fatalf("call Git Branch expected to be (%v) but was (%v)", expectedBranch, captureBranch) |
| 166 | + } |
| 167 | + if captureDir, expectedDir := pointer.StringPtrDerefOr(captureFn.Git.ContextDir, ""), pointer.StringPtrDerefOr(tt.expectCallContextDir, ""); captureDir != expectedDir { |
| 168 | + t.Fatalf("call Git Dir expected to be (%v) but was (%v)", expectedDir, captureDir) |
| 169 | + } |
| 170 | + } |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments