GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — func/cmd/config_ci_int_test.go at main · stackbuilders/func Expand file tree/ Copy pathconfig_ci_int_test.goMore file actions 263 lines (206 loc) · 7.82 KB / Copy pathconfig_ci_int_test.goFile metadata and controls263 lines (206 loc) · 7.82 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 package cmd_test import ( "errors" "fmt" "os" "path/filepath" "slices" "strings" "testing" "github.com/ory/viper" "gotest.tools/v3/assert" "gotest.tools/v3/assert/cmp" fnCmd "knative.dev/func/cmd" "knative.dev/func/cmd/common" cmdTest "knative.dev/func/cmd/testing" "knative.dev/func/pkg/ci/github" fn "knative.dev/func/pkg/functions" fnTest "knative.dev/func/pkg/testing" "knative.dev/func/pkg/version" ) // START: Integration Tests // ------------------------ // No more mocking. Using real filesystem here for LoaderSaver and WorkflowWriter. func TestNewConfigCICmd_FailsWhenNotInitialized(t *testing.T) { // passing empty func &fn.Function{} means no func will be initialized // in temp dir opts := defaultIntegrationOpts(t, &fn.Function{}) _, gitInitErr := common.NewGitCliWrapper().Init(fnTest.FromTempDirectory(t), mainBranch) expectedErr := fn.NewErrNotInitialized(fnTest.Cwd()) err := runConfigCiCmdIntegration(t, opts) assert.NilError(t, gitInitErr) assert.Error(t, err, expectedErr.Error()) } func TestNewConfigCICmd_SuccessWhenInitialized(t *testing.T) { opts := defaultIntegrationOpts(t, nil) err := runConfigCiCmdIntegration(t, opts) assert.NilError(t, err) } func TestNewConfigCICmd_FailsToLoadFuncWithWrongPath(t *testing.T) { opts := defaultIntegrationOpts(t, nil) opts.args = append(opts.args, "--path=nofunc") var expectedErr *os.PathError err := runConfigCiCmdIntegration(t, opts) // Use os.IsNotExist for cross-platform compatibility (Linux vs Windows error messages differ) assert.Assert(t, errors.As(err, &expectedErr)) assert.Assert(t, os.IsNotExist(expectedErr)) } func TestNewConfigCICmd_SuccessfulLoadWithCorrectPath(t *testing.T) { f := cmdTest.CreateFuncWithGitInTempDir(t, fnName) opts := defaultIntegrationOpts(t, &f) opts.args = append(opts.args, "--path="+f.Root) err := runConfigCiCmdIntegration(t, opts) assert.NilError(t, err) } func TestNewConfigCICmd_CreatesGitHubWorkflowDirectory(t *testing.T) { opts := defaultIntegrationOpts(t, nil) err := runConfigCiCmdIntegration(t, opts) assert.NilError(t, err) _, statErr := os.Stat(filepath.Join(opts.withFunc.Root, github.DefaultGitHubWorkflowDir)) assert.NilError(t, statErr) } func TestNewConfigCICmd_WritesWorkflowFileToFSWithCorrectYAMLStructure(t *testing.T) { // Pin the CLI version so the generated workflow is deterministic, // independent of the version injected via -ldflags at build time // (which varies with the branch's most recent git tag). prev := version.Kver version.Kver = github.DefaultFuncCliVersion t.Cleanup(func() { version.Kver = prev }) opts := defaultIntegrationOpts(t, nil) err := runConfigCiCmdIntegration(t, opts) raw := readWorkflowFile(t, opts.withFunc.Root) assert.NilError(t, err) assertDefaultWorkflow(t, raw) } func TestNewConfigCICmd_ForceFlagOverwritesExistingWorkflowOnFS(t *testing.T) { workflowName := "Func Deploy" changedWorkflowName := "Sales Service Deployment" baseOpts := defaultIntegrationOpts(t, nil) t.Run("initial workflow creation succeeds", func(t *testing.T) { err := runConfigCiCmdIntegration(t, baseOpts) content := readWorkflowFile(t, baseOpts.withFunc.Root) assert.NilError(t, err) assert.Assert(t, yamlContains(content, workflowName)) }) t.Run("overwrite without force flag fails", func(t *testing.T) { opts := optsIntegration{ withFunc: baseOpts.withFunc, args: append(slices.Clone(baseOpts.args), "--workflow-name="+changedWorkflowName), } err := runConfigCiCmdIntegration(t, opts) content := readWorkflowFile(t, opts.withFunc.Root) assert.ErrorIs(t, err, github.ErrWorkflowExists) assert.Assert(t, yamlContains(content, workflowName)) assert.Assert(t, !strings.Contains(content, changedWorkflowName)) }) t.Run("overwrite with force flag succeeds", func(t *testing.T) { opts := optsIntegration{ withFunc: baseOpts.withFunc, args: append(slices.Clone(baseOpts.args), "--workflow-name="+changedWorkflowName, "--force"), } err := runConfigCiCmdIntegration(t, opts) content := readWorkflowFile(t, opts.withFunc.Root) assert.NilError(t, err) assert.Assert(t, yamlContains(content, changedWorkflowName)) assert.Assert(t, !strings.Contains(content, workflowName)) }) } // ---------------------- // END: Integration Tests // START: Testing Framework // ------------------------ const fnName = "github-ci-func" type optsIntegration struct { withFunc *fn.Function args []string } // defaultIntegrationOpts returns test options for integration tests with sensible defaults: // - withFunc: provided f or newly created function with git in temp directory // - args: []string{"ci"} // // If f is nil, creates a new function with git in a temp directory. // If f is provided, uses that function (allows custom path testing). func defaultIntegrationOpts(t *testing.T, f *fn.Function) optsIntegration { t.Helper() aFunc := f if f == nil { fnTmp := cmdTest.CreateFuncWithGitInTempDir(t, fnName) aFunc = &fnTmp } return optsIntegration{ withFunc: aFunc, args: []string{"ci"}, } } func runConfigCiCmdIntegration( t *testing.T, opts optsIntegration, ) error { t.Helper() // PRE-RUN PREP // all options for "func config ci" command t.Setenv(fnCmd.ConfigCIFeatureFlag, "true") args := opts.args if len(opts.args) == 0 { args = []string{"ci"} } viper.Reset() cmd := fnCmd.NewConfigCmd( common.DefaultLoaderSaver, github.DefaultWorkflowWriter, common.DefaultCurrentBranch, common.DefaultWorkDir, fnCmd.NewCIGeneratorFactory(), fnCmd.NewClient, ) cmd.SetArgs(args) // RUN return cmd.Execute() } func readWorkflowFile(t *testing.T, root string) string { t.Helper() path := filepath.Join(root, github.DefaultGitHubWorkflowDir, github.DefaultGitHubWorkflowFilename) result, err := os.ReadFile(path) assert.NilError(t, err) return string(result) } func assertDefaultWorkflow(t *testing.T, actualGw string) { t.Helper() assert.Assert(t, yamlContains(actualGw, "Func Deploy")) assert.Assert(t, yamlContains(actualGw, "- main")) assert.Assert(t, yamlContains(actualGw, "ubuntu-latest")) assert.Assert(t, strings.Count(actualGw, "- name:") == 6) assert.Assert(t, yamlContains(actualGw, "Checkout code")) assert.Assert(t, yamlContains(actualGw, "actions/checkout@v4")) assert.Assert(t, yamlContains(actualGw, "Run tests")) assert.Assert(t, yamlContains(actualGw, "go test ./...")) assert.Assert(t, yamlContains(actualGw, "Setup Kubernetes context")) assert.Assert(t, yamlContains(actualGw, "azure/k8s-set-context@v4")) assert.Assert(t, yamlContains(actualGw, "method: kubeconfig")) assert.Assert(t, yamlContains(actualGw, "kubeconfig: ${{ secrets.KUBECONFIG }}")) assert.Assert(t, yamlContains(actualGw, "Login to container registry")) assert.Assert(t, yamlContains(actualGw, "docker/login-action@v3")) assert.Assert(t, yamlContains(actualGw, "registry: ${{ vars.REGISTRY_LOGIN_URL }}")) assert.Assert(t, yamlContains(actualGw, "username: ${{ vars.REGISTRY_USERNAME }}")) assert.Assert(t, yamlContains(actualGw, "password: ${{ secrets.REGISTRY_PASSWORD }}")) assert.Assert(t, yamlContains(actualGw, "Install func cli")) assert.Assert(t, yamlContains(actualGw, "functions-dev/action@main")) assert.Assert(t, yamlContains(actualGw, "version: "+github.DefaultFuncCliVersion)) assert.Assert(t, yamlContains(actualGw, "name: func")) assert.Assert(t, yamlContains(actualGw, "Deploy function")) assert.Assert(t, yamlContains(actualGw, `FUNC_VERBOSE: "true"`)) assert.Assert(t, yamlContains(actualGw, "FUNC_REGISTRY: ${{ vars.REGISTRY_LOGIN_URL }}/${{ vars.REGISTRY_USERNAME }}")) assert.Assert(t, yamlContains(actualGw, "func deploy")) } func yamlContains(yaml, substr string) cmp.Comparison { return func() cmp.Result { if strings.Contains(yaml, substr) { return cmp.ResultSuccess } return cmp.ResultFailure(fmt.Sprintf( "missing '%s' in:\n\n%s", substr, yaml, )) } } // ---------------------- // END: Testing Framework |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |