|
| 1 | +package dockerutil_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/spf13/afero" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/coder/envbox/dockerutil" |
| 14 | + "github.com/coder/envbox/xunix" |
| 15 | + "github.com/coder/envbox/xunix/xunixfake" |
| 16 | +) |
| 17 | + |
| 18 | +func TestWriteCertsForRegistry(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + t.Run("SingleCertFile", func(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + // Test setup |
| 24 | + fs := xunixfake.NewMemFS() |
| 25 | + ctx := xunix.WithFS(context.Background(), fs) |
| 26 | + |
| 27 | + // Create a test certificate file |
| 28 | + certContent := []byte("test certificate content") |
| 29 | + err := afero.WriteFile(fs, "/certs/ca.crt", certContent, 0o644) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + // Run the function |
| 33 | + err = dockerutil.WriteCertsForRegistry(ctx, "test.registry.com", "/certs/ca.crt") |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + // Check the result |
| 37 | + copiedContent, err := afero.ReadFile(fs, "/etc/docker/certs.d/test.registry.com/ca.crt") |
| 38 | + require.NoError(t, err) |
| 39 | + assert.Equal(t, certContent, copiedContent) |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("MultipleCertFiles", func(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + // Test setup |
| 45 | + fs := xunixfake.NewMemFS() |
| 46 | + ctx := xunix.WithFS(context.Background(), fs) |
| 47 | + |
| 48 | + // Create test certificate files |
| 49 | + certFiles := []string{"ca.crt", "client.cert", "client.key"} |
| 50 | + for _, file := range certFiles { |
| 51 | + err := afero.WriteFile(fs, filepath.Join("/certs", file), []byte("content of "+file), 0o644) |
| 52 | + require.NoError(t, err) |
| 53 | + } |
| 54 | + |
| 55 | + // Run the function |
| 56 | + err := dockerutil.WriteCertsForRegistry(ctx, "test.registry.com", "/certs") |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + // Check the results |
| 60 | + for _, file := range certFiles { |
| 61 | + copiedContent, err := afero.ReadFile(fs, filepath.Join("/etc/docker/certs.d/test.registry.com", file)) |
| 62 | + require.NoError(t, err) |
| 63 | + assert.Equal(t, []byte("content of "+file), copiedContent) |
| 64 | + } |
| 65 | + }) |
| 66 | + t.Run("ExistingRegistryCertsDir", func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + // Test setup |
| 69 | + fs := xunixfake.NewMemFS() |
| 70 | + ctx := xunix.WithFS(context.Background(), fs) |
| 71 | + |
| 72 | + // Create an existing registry certs directory |
| 73 | + registryCertsDir := "/etc/docker/certs.d/test.registry.com" |
| 74 | + err := fs.MkdirAll(registryCertsDir, 0o755) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Create a file in the existing directory |
| 78 | + existingContent := []byte("existing certificate content") |
| 79 | + err = afero.WriteFile(fs, filepath.Join(registryCertsDir, "existing.crt"), existingContent, 0o644) |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + // Create a test certificate file in the source directory |
| 83 | + certContent := []byte("new certificate content") |
| 84 | + err = afero.WriteFile(fs, "/certs/ca.crt", certContent, 0o644) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + // Run the function |
| 88 | + err = dockerutil.WriteCertsForRegistry(ctx, "test.registry.com", "/certs") |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + // Check that the existing file was not modified |
| 92 | + existingFileContent, err := afero.ReadFile(fs, filepath.Join(registryCertsDir, "existing.crt")) |
| 93 | + require.NoError(t, err) |
| 94 | + assert.Equal(t, existingContent, existingFileContent) |
| 95 | + |
| 96 | + // Check that the new file was not copied |
| 97 | + _, err = fs.Stat(filepath.Join(registryCertsDir, "ca.crt")) |
| 98 | + assert.True(t, os.IsNotExist(err), "New certificate file should not have been copied") |
| 99 | + }) |
| 100 | +} |
0 commit comments