|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/pflag" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +// executeAndResetCommand is a test helper that runs and then resets a command for executing in another test. |
| 18 | +func executeAndResetCommand(ctx context.Context, cmd *cobra.Command, args []string) (string, string, error) { |
| 19 | + beforeOut, beforeErr := cmd.OutOrStderr(), cmd.ErrOrStderr() |
| 20 | + defer func() { |
| 21 | + cmd.SetOut(beforeOut) |
| 22 | + cmd.SetErr(beforeErr) |
| 23 | + }() |
| 24 | + |
| 25 | + nowOut, nowErr := new(bytes.Buffer), new(bytes.Buffer) |
| 26 | + cmd.SetOut(nowOut) |
| 27 | + cmd.SetErr(nowErr) |
| 28 | + cmd.SetArgs(args) |
| 29 | + subCmd, err := cmd.ExecuteContextC(ctx) |
| 30 | + if subCmd != nil { |
| 31 | + subCmd.SetContext(nil) |
| 32 | + subCmd.SilenceUsage = false |
| 33 | + subCmd.SilenceErrors = false |
| 34 | + subCmd.Flags().VisitAll(func(f *pflag.Flag) { |
| 35 | + _ = f.Value.Set(f.DefValue) |
| 36 | + }) |
| 37 | + } |
| 38 | + return nowOut.String(), nowErr.String(), err |
| 39 | +} |
| 40 | + |
| 41 | +func TestExample(t *testing.T) { |
| 42 | + td := t.TempDir() |
| 43 | + require.NoError(t, os.WriteFile(filepath.Join(td, "score.yaml"), []byte(` |
| 44 | +apiVersion: score.dev/v1b1 |
| 45 | +metadata: |
| 46 | + name: example-workload-name123 |
| 47 | + extra-key: extra-value |
| 48 | +service: |
| 49 | + ports: |
| 50 | + port-one: |
| 51 | + port: 1000 |
| 52 | + protocol: TCP |
| 53 | + targetPort: 10000 |
| 54 | + port-two2: |
| 55 | + port: 8000 |
| 56 | +containers: |
| 57 | + container-one1: |
| 58 | + image: localhost:4000/repo/my-image:tag |
| 59 | + command: ["/bin/sh", "-c"] |
| 60 | + args: ["hello", "world"] |
| 61 | + resources: |
| 62 | + requests: |
| 63 | + cpu: 1000m |
| 64 | + memory: 10Gi |
| 65 | + limits: |
| 66 | + cpu: "0.24" |
| 67 | + memory: 128M |
| 68 | + variables: |
| 69 | + SOME_VAR: some content here |
| 70 | + volumes: |
| 71 | + - source: volume-name |
| 72 | + target: /mnt/something |
| 73 | + readOnly: false |
| 74 | + - source: volume-two |
| 75 | + target: /mnt/something-else |
| 76 | + livenessProbe: |
| 77 | + httpGet: |
| 78 | + port: 8080 |
| 79 | + path: /livez |
| 80 | + readinessProbe: |
| 81 | + httpGet: |
| 82 | + host: 127.0.0.1 |
| 83 | + port: 80 |
| 84 | + scheme: HTTP |
| 85 | + path: /readyz |
| 86 | + httpHeaders: |
| 87 | + - name: SOME_HEADER |
| 88 | + value: some-value-here |
| 89 | + container-two2: |
| 90 | + image: localhost:4000/repo/my-image:tag |
| 91 | +resources: |
| 92 | + resource-one1: |
| 93 | + metadata: |
| 94 | + annotations: |
| 95 | + Default-Annotation: this is my annotation |
| 96 | + prefix.com/Another-Key_Annotation.2: something else |
| 97 | + extra-key: extra-value |
| 98 | + type: Resource-One |
| 99 | + class: default |
| 100 | + params: |
| 101 | + extra: |
| 102 | + data: here |
| 103 | + resource-two2: |
| 104 | + type: Resource-Two |
| 105 | +`), 0600)) |
| 106 | + stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"run", "--file", filepath.Join(td, "score.yaml"), "--output", filepath.Join(td, "compose.yaml")}) |
| 107 | + assert.NoError(t, err) |
| 108 | + assert.NotEqual(t, "", stdout) |
| 109 | + assert.Equal(t, "", stderr) |
| 110 | + rawComposeContent, err := os.ReadFile(filepath.Join(td, "compose.yaml")) |
| 111 | + require.NoError(t, err) |
| 112 | + var actualComposeContent map[string]interface{} |
| 113 | + assert.NoError(t, yaml.Unmarshal(rawComposeContent, &actualComposeContent)) |
| 114 | + assert.Equal(t, map[string]interface{}{ |
| 115 | + "services": map[string]interface{}{ |
| 116 | + "example-workload-name123-container-one1": map[string]interface{}{ |
| 117 | + "image": "localhost:4000/repo/my-image:tag", |
| 118 | + "entrypoint": []interface{}{"/bin/sh", "-c"}, |
| 119 | + "command": []interface{}{"hello", "world"}, |
| 120 | + "environment": map[string]interface{}{ |
| 121 | + "SOME_VAR": "some content here", |
| 122 | + }, |
| 123 | + "ports": []interface{}{ |
| 124 | + map[string]interface{}{"target": 10000, "published": "1000", "protocol": "TCP"}, |
| 125 | + map[string]interface{}{"target": 8000, "published": "8000"}, |
| 126 | + }, |
| 127 | + "volumes": []interface{}{ |
| 128 | + map[string]interface{}{"type": "volume", "source": "volume-name", "target": "/mnt/something"}, |
| 129 | + map[string]interface{}{"type": "volume", "source": "volume-two", "target": "/mnt/something-else"}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + "example-workload-name123-container-two2": map[string]interface{}{ |
| 133 | + "image": "localhost:4000/repo/my-image:tag", |
| 134 | + "network_mode": "service:example-workload-name123-container-one1", |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, actualComposeContent) |
| 138 | +} |
| 139 | + |
| 140 | +func TestExample_invalid_spec(t *testing.T) { |
| 141 | + td := t.TempDir() |
| 142 | + require.NoError(t, os.WriteFile(filepath.Join(td, "score.yaml"), []byte(` |
| 143 | +{}`), 0600)) |
| 144 | + stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"run", "--file", filepath.Join(td, "score.yaml"), "--output", filepath.Join(td, "compose.yaml")}) |
| 145 | + assert.EqualError(t, err, "validating workload spec: jsonschema: '' does not validate with https://score.dev/schemas/score#/required: missing properties: 'apiVersion', 'metadata', 'containers'") |
| 146 | + assert.Equal(t, "", stdout) |
| 147 | + assert.Equal(t, "", stderr) |
| 148 | +} |
0 commit comments