|
| 1 | +package modulecmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "cuelang.org/go/cue/cuecontext" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/opmodel/cli/internal/config" |
| 15 | +) |
| 16 | + |
| 17 | +func TestNewModuleCRDCmd(t *testing.T) { |
| 18 | + cmd := NewModuleCRDCmd(&config.GlobalConfig{}) |
| 19 | + |
| 20 | + assert.Equal(t, "crd [path]", cmd.Use) |
| 21 | + assert.NotEmpty(t, cmd.Short) |
| 22 | + assert.NotEmpty(t, cmd.Long) |
| 23 | + |
| 24 | + groupFlag := cmd.Flags().Lookup("group") |
| 25 | + require.NotNil(t, groupFlag) |
| 26 | + assert.Equal(t, "module.opmodel.dev", groupFlag.DefValue) |
| 27 | + |
| 28 | + outputFlag := cmd.Flags().Lookup("output") |
| 29 | + require.NotNil(t, outputFlag) |
| 30 | + assert.Equal(t, "o", outputFlag.Shorthand) |
| 31 | + assert.Equal(t, "yaml", outputFlag.DefValue) |
| 32 | +} |
| 33 | + |
| 34 | +// TestModCRD_SimpleModule runs the command against the simple-module fixture |
| 35 | +// and verifies the YAML on stdout looks like a CRD. End-to-end coverage of |
| 36 | +// flag → load → BuildCRD → output wiring; the schema/name details are |
| 37 | +// exercised by pkg/crd tests. |
| 38 | +func TestModCRD_SimpleModule(t *testing.T) { |
| 39 | + fixtureDir := filepath.Join("..", "..", "..", "tests", "fixtures", "valid", "simple-module") |
| 40 | + if _, err := os.Stat(fixtureDir); os.IsNotExist(err) { |
| 41 | + t.Skip("Test fixture not found:", fixtureDir) |
| 42 | + } |
| 43 | + |
| 44 | + tmpHome, cleanup := setupTestConfig(t) |
| 45 | + defer cleanup() |
| 46 | + |
| 47 | + origHome := os.Getenv("HOME") |
| 48 | + os.Setenv("HOME", tmpHome) |
| 49 | + defer os.Setenv("HOME", origHome) |
| 50 | + |
| 51 | + os.Unsetenv("OPM_REGISTRY") |
| 52 | + |
| 53 | + cfg := &config.GlobalConfig{CueContext: cuecontext.New()} |
| 54 | + cmd := NewModuleCRDCmd(cfg) |
| 55 | + cmd.SetOut(&bytes.Buffer{}) |
| 56 | + cmd.SetErr(&bytes.Buffer{}) |
| 57 | + cmd.SetArgs([]string{fixtureDir}) |
| 58 | + |
| 59 | + stdout := captureStdout(t, func() { |
| 60 | + require.NoError(t, cmd.Execute()) |
| 61 | + }) |
| 62 | + |
| 63 | + // Derived from simple-module's metadata.name ("simple-module"), version ("0.1.0"): |
| 64 | + // kind = SimpleModule, plural = simplemodules, version = v1alpha1. |
| 65 | + assert.Contains(t, stdout, "apiVersion: apiextensions.k8s.io/v1") |
| 66 | + assert.Contains(t, stdout, "kind: CustomResourceDefinition") |
| 67 | + assert.Contains(t, stdout, "name: simplemodules.module.opmodel.dev") |
| 68 | + assert.Contains(t, stdout, "kind: SimpleModule") |
| 69 | + assert.Contains(t, stdout, "listKind: SimpleModuleList") |
| 70 | + assert.Contains(t, stdout, "scope: Namespaced") |
| 71 | + assert.Contains(t, stdout, "name: v1alpha1") |
| 72 | + assert.Contains(t, stdout, "openAPIV3Schema") |
| 73 | + |
| 74 | + // Provenance labels and annotations link the CRD back to the module. |
| 75 | + assert.Contains(t, stdout, "app.kubernetes.io/managed-by: opm-cli") |
| 76 | + assert.Contains(t, stdout, "module.opmodel.dev/name: simple-module") |
| 77 | + assert.Contains(t, stdout, "module.opmodel.dev/version: 0.1.0") |
| 78 | + // simple-module declares modulePath and fqn but not description or uuid. |
| 79 | + assert.Contains(t, stdout, "module.opmodel.dev/path: example.com/modules") |
| 80 | + assert.Contains(t, stdout, "module.opmodel.dev/fqn: example.com/modules/simple-module:0.1.0") |
| 81 | + assert.NotContains(t, stdout, "module.opmodel.dev/description") |
| 82 | + assert.NotContains(t, stdout, "module.opmodel.dev/uuid") |
| 83 | +} |
| 84 | + |
| 85 | +func TestModCRD_CustomGroup(t *testing.T) { |
| 86 | + fixtureDir := filepath.Join("..", "..", "..", "tests", "fixtures", "valid", "simple-module") |
| 87 | + if _, err := os.Stat(fixtureDir); os.IsNotExist(err) { |
| 88 | + t.Skip("Test fixture not found:", fixtureDir) |
| 89 | + } |
| 90 | + |
| 91 | + tmpHome, cleanup := setupTestConfig(t) |
| 92 | + defer cleanup() |
| 93 | + |
| 94 | + origHome := os.Getenv("HOME") |
| 95 | + os.Setenv("HOME", tmpHome) |
| 96 | + defer os.Setenv("HOME", origHome) |
| 97 | + |
| 98 | + os.Unsetenv("OPM_REGISTRY") |
| 99 | + |
| 100 | + cfg := &config.GlobalConfig{CueContext: cuecontext.New()} |
| 101 | + cmd := NewModuleCRDCmd(cfg) |
| 102 | + cmd.SetOut(&bytes.Buffer{}) |
| 103 | + cmd.SetErr(&bytes.Buffer{}) |
| 104 | + cmd.SetArgs([]string{fixtureDir, "--group", "example.com"}) |
| 105 | + |
| 106 | + stdout := captureStdout(t, func() { |
| 107 | + require.NoError(t, cmd.Execute()) |
| 108 | + }) |
| 109 | + |
| 110 | + assert.Contains(t, stdout, "name: simplemodules.example.com") |
| 111 | + assert.Contains(t, stdout, "group: example.com") |
| 112 | + // The CRD's own group should not fall back to the default. Provenance |
| 113 | + // labels on metadata.labels may still reference module.opmodel.dev/*, |
| 114 | + // so check group specifically rather than the raw string. |
| 115 | + assert.NotContains(t, stdout, "group: opmodel.dev") |
| 116 | +} |
| 117 | + |
| 118 | +func TestModCRD_JSONOutput(t *testing.T) { |
| 119 | + fixtureDir := filepath.Join("..", "..", "..", "tests", "fixtures", "valid", "simple-module") |
| 120 | + if _, err := os.Stat(fixtureDir); os.IsNotExist(err) { |
| 121 | + t.Skip("Test fixture not found:", fixtureDir) |
| 122 | + } |
| 123 | + |
| 124 | + tmpHome, cleanup := setupTestConfig(t) |
| 125 | + defer cleanup() |
| 126 | + |
| 127 | + origHome := os.Getenv("HOME") |
| 128 | + os.Setenv("HOME", tmpHome) |
| 129 | + defer os.Setenv("HOME", origHome) |
| 130 | + |
| 131 | + os.Unsetenv("OPM_REGISTRY") |
| 132 | + |
| 133 | + cfg := &config.GlobalConfig{CueContext: cuecontext.New()} |
| 134 | + cmd := NewModuleCRDCmd(cfg) |
| 135 | + cmd.SetOut(&bytes.Buffer{}) |
| 136 | + cmd.SetErr(&bytes.Buffer{}) |
| 137 | + cmd.SetArgs([]string{fixtureDir, "-o", "json"}) |
| 138 | + |
| 139 | + stdout := captureStdout(t, func() { |
| 140 | + require.NoError(t, cmd.Execute()) |
| 141 | + }) |
| 142 | + |
| 143 | + assert.Contains(t, stdout, `"apiVersion": "apiextensions.k8s.io/v1"`) |
| 144 | + assert.Contains(t, stdout, `"kind": "CustomResourceDefinition"`) |
| 145 | +} |
| 146 | + |
| 147 | +func TestModCRD_InvalidPath(t *testing.T) { |
| 148 | + tmpHome, cleanup := setupTestConfig(t) |
| 149 | + defer cleanup() |
| 150 | + |
| 151 | + origHome := os.Getenv("HOME") |
| 152 | + os.Setenv("HOME", tmpHome) |
| 153 | + defer os.Setenv("HOME", origHome) |
| 154 | + |
| 155 | + cfg := &config.GlobalConfig{CueContext: cuecontext.New()} |
| 156 | + cmd := NewModuleCRDCmd(cfg) |
| 157 | + cmd.SetOut(&bytes.Buffer{}) |
| 158 | + cmd.SetErr(&bytes.Buffer{}) |
| 159 | + cmd.SetArgs([]string{"/does/not/exist"}) |
| 160 | + |
| 161 | + err := cmd.Execute() |
| 162 | + require.Error(t, err) |
| 163 | +} |
| 164 | + |
| 165 | +func TestModCRD_InvalidOutputFormat(t *testing.T) { |
| 166 | + cfg := &config.GlobalConfig{CueContext: cuecontext.New()} |
| 167 | + cmd := NewModuleCRDCmd(cfg) |
| 168 | + cmd.SetOut(&bytes.Buffer{}) |
| 169 | + cmd.SetErr(&bytes.Buffer{}) |
| 170 | + cmd.SetArgs([]string{".", "-o", "xml"}) |
| 171 | + |
| 172 | + err := cmd.Execute() |
| 173 | + require.Error(t, err) |
| 174 | + assert.Contains(t, err.Error(), "invalid output format") |
| 175 | +} |
| 176 | + |
| 177 | +// captureStdout replaces os.Stdout with a pipe for the duration of fn and |
| 178 | +// returns whatever was written. WriteManifestOutput writes directly to |
| 179 | +// os.Stdout, so this is the narrowest way to assert on its output. |
| 180 | +// Tests that call this must not run with t.Parallel() — os.Stdout is |
| 181 | +// process-global. |
| 182 | +func captureStdout(t *testing.T, fn func()) string { |
| 183 | + t.Helper() |
| 184 | + |
| 185 | + orig := os.Stdout |
| 186 | + r, w, err := os.Pipe() |
| 187 | + require.NoError(t, err) |
| 188 | + os.Stdout = w |
| 189 | + |
| 190 | + done := make(chan []byte, 1) |
| 191 | + go func() { |
| 192 | + var buf bytes.Buffer |
| 193 | + _, _ = io.Copy(&buf, r) |
| 194 | + done <- buf.Bytes() |
| 195 | + }() |
| 196 | + |
| 197 | + defer func() { |
| 198 | + os.Stdout = orig |
| 199 | + }() |
| 200 | + |
| 201 | + fn() |
| 202 | + |
| 203 | + require.NoError(t, w.Close()) |
| 204 | + out := <-done |
| 205 | + return string(out) |
| 206 | +} |
0 commit comments