|
| 1 | +package state |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + |
| 7 | + runtime "github.com/container-registry/harbor-satellite/internal/container_runtime" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestFormatCRIActivity(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + results []runtime.CRIConfigResult |
| 15 | + want string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "single success with backup", |
| 19 | + results: []runtime.CRIConfigResult{ |
| 20 | + {CRI: runtime.CRIDocker, Success: true, BackupPath: "/etc/docker/daemon.json.bak.20250129T100000"}, |
| 21 | + }, |
| 22 | + want: "cri_fallback_configured: docker(ok, backup:/etc/docker/daemon.json.bak.20250129T100000)", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "single success without backup", |
| 26 | + results: []runtime.CRIConfigResult{ |
| 27 | + {CRI: runtime.CRIContainerd, Success: true}, |
| 28 | + }, |
| 29 | + want: "cri_fallback_configured: containerd(ok)", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "single failure", |
| 33 | + results: []runtime.CRIConfigResult{ |
| 34 | + {CRI: runtime.CRIDocker, Success: false, Error: "failed to restart Docker"}, |
| 35 | + }, |
| 36 | + want: "cri_fallback_configured: docker(err:failed to restart Docker)", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "mixed results", |
| 40 | + results: []runtime.CRIConfigResult{ |
| 41 | + {CRI: runtime.CRIDocker, Success: true, BackupPath: "/etc/docker/daemon.json.bak.20250129T100000"}, |
| 42 | + {CRI: runtime.CRIContainerd, Success: true}, |
| 43 | + {CRI: runtime.CRICrio, Success: false, Error: "permission denied"}, |
| 44 | + }, |
| 45 | + want: "cri_fallback_configured: docker(ok, backup:/etc/docker/daemon.json.bak.20250129T100000), containerd(ok), crio(err:permission denied)", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "empty results", |
| 49 | + results: []runtime.CRIConfigResult{}, |
| 50 | + want: "cri_fallback_configured: ", |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tt := range tests { |
| 55 | + t.Run(tt.name, func(t *testing.T) { |
| 56 | + got := formatCRIActivity(tt.results) |
| 57 | + require.Equal(t, tt.want, got) |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestSetPendingCRIResults(t *testing.T) { |
| 63 | + t.Run("stores results", func(t *testing.T) { |
| 64 | + p := &StatusReportingProcess{ |
| 65 | + name: "test", |
| 66 | + mu: &sync.Mutex{}, |
| 67 | + } |
| 68 | + results := []runtime.CRIConfigResult{ |
| 69 | + {CRI: runtime.CRIDocker, Success: true}, |
| 70 | + } |
| 71 | + |
| 72 | + p.SetPendingCRIResults(results) |
| 73 | + |
| 74 | + p.mu.Lock() |
| 75 | + defer p.mu.Unlock() |
| 76 | + require.Len(t, p.pendingCRI, 1) |
| 77 | + require.Equal(t, runtime.CRIDocker, p.pendingCRI[0].CRI) |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("overwrites previous results", func(t *testing.T) { |
| 81 | + p := &StatusReportingProcess{ |
| 82 | + name: "test", |
| 83 | + mu: &sync.Mutex{}, |
| 84 | + } |
| 85 | + |
| 86 | + p.SetPendingCRIResults([]runtime.CRIConfigResult{ |
| 87 | + {CRI: runtime.CRIDocker, Success: true}, |
| 88 | + }) |
| 89 | + p.SetPendingCRIResults([]runtime.CRIConfigResult{ |
| 90 | + {CRI: runtime.CRIContainerd, Success: false, Error: "fail"}, |
| 91 | + }) |
| 92 | + |
| 93 | + p.mu.Lock() |
| 94 | + defer p.mu.Unlock() |
| 95 | + require.Len(t, p.pendingCRI, 1) |
| 96 | + require.Equal(t, runtime.CRIContainerd, p.pendingCRI[0].CRI) |
| 97 | + }) |
| 98 | + |
| 99 | + t.Run("nil results clears pending", func(t *testing.T) { |
| 100 | + p := &StatusReportingProcess{ |
| 101 | + name: "test", |
| 102 | + mu: &sync.Mutex{}, |
| 103 | + } |
| 104 | + p.SetPendingCRIResults([]runtime.CRIConfigResult{ |
| 105 | + {CRI: runtime.CRIDocker, Success: true}, |
| 106 | + }) |
| 107 | + p.SetPendingCRIResults(nil) |
| 108 | + |
| 109 | + p.mu.Lock() |
| 110 | + defer p.mu.Unlock() |
| 111 | + require.Nil(t, p.pendingCRI) |
| 112 | + }) |
| 113 | +} |
0 commit comments