|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + apps "github.com/ninech/apis/apps/v1alpha1" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "k8s.io/utils/ptr" |
| 9 | +) |
| 10 | + |
| 11 | +func TestApplyProbePatch(t *testing.T) { |
| 12 | + setPath := func(s string) OptString { |
| 13 | + return OptString{State: Set, Val: s} |
| 14 | + } |
| 15 | + clearPath := func() OptString { |
| 16 | + return OptString{State: Clear} |
| 17 | + } |
| 18 | + unsetPath := func() OptString { |
| 19 | + return OptString{State: Unset} |
| 20 | + } |
| 21 | + setPer := func(n int32) OptInt32 { |
| 22 | + return OptInt32{State: Set, Val: n} |
| 23 | + } |
| 24 | + clearPer := func() OptInt32 { |
| 25 | + return OptInt32{State: Clear} |
| 26 | + } |
| 27 | + unsetPer := func() OptInt32 { |
| 28 | + return OptInt32{State: Unset} |
| 29 | + } |
| 30 | + |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + cfg apps.Config |
| 34 | + pp ProbePatch |
| 35 | + want func(t *testing.T, got *apps.Config) |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "no-op when everything Unset and cfg nil", |
| 39 | + cfg: apps.Config{}, |
| 40 | + pp: ProbePatch{Path: unsetPath(), PeriodSeconds: unsetPer()}, |
| 41 | + want: func(t *testing.T, got *apps.Config) { |
| 42 | + assert.Nil(t, got.HealthProbe) |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "set Path creates probe+httpget and assigns path", |
| 47 | + cfg: apps.Config{}, |
| 48 | + pp: ProbePatch{Path: setPath("/healthz"), PeriodSeconds: unsetPer()}, |
| 49 | + want: func(t *testing.T, got *apps.Config) { |
| 50 | + if assert.NotNil(t, got.HealthProbe) && assert.NotNil(t, got.HealthProbe.HTTPGet) { |
| 51 | + assert.Equal(t, "/healthz", got.HealthProbe.HTTPGet.Path) |
| 52 | + } |
| 53 | + assert.Nil(t, got.HealthProbe.PeriodSeconds) |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "set PeriodSeconds creates probe and sets value (owns memory)", |
| 58 | + cfg: apps.Config{}, |
| 59 | + pp: ProbePatch{Path: unsetPath(), PeriodSeconds: setPer(7)}, |
| 60 | + want: func(t *testing.T, got *apps.Config) { |
| 61 | + if assert.NotNil(t, got.HealthProbe) { |
| 62 | + if assert.NotNil(t, got.HealthProbe.PeriodSeconds) { |
| 63 | + assert.Equal(t, int32(7), *got.HealthProbe.PeriodSeconds) |
| 64 | + } |
| 65 | + assert.Nil(t, got.HealthProbe.HTTPGet) |
| 66 | + } |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "set both fields on existing probe updates both", |
| 71 | + cfg: apps.Config{ |
| 72 | + HealthProbe: &apps.Probe{ |
| 73 | + ProbeHandler: apps.ProbeHandler{ |
| 74 | + HTTPGet: &apps.HTTPGetAction{Path: "/old"}, |
| 75 | + }, |
| 76 | + PeriodSeconds: ptr.To(int32(3)), |
| 77 | + }, |
| 78 | + }, |
| 79 | + pp: ProbePatch{Path: setPath("/new"), PeriodSeconds: setPer(9)}, |
| 80 | + want: func(t *testing.T, got *apps.Config) { |
| 81 | + if assert.NotNil(t, got.HealthProbe) && assert.NotNil(t, got.HealthProbe.HTTPGet) { |
| 82 | + assert.Equal(t, "/new", got.HealthProbe.HTTPGet.Path) |
| 83 | + } |
| 84 | + if assert.NotNil(t, got.HealthProbe.PeriodSeconds) { |
| 85 | + assert.Equal(t, int32(9), *got.HealthProbe.PeriodSeconds) |
| 86 | + } |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "clear Path removes HTTPGet but keeps probe if other fields remain", |
| 91 | + cfg: apps.Config{ |
| 92 | + HealthProbe: &apps.Probe{ |
| 93 | + ProbeHandler: apps.ProbeHandler{ |
| 94 | + HTTPGet: &apps.HTTPGetAction{Path: "/keep-me?no"}, |
| 95 | + }, |
| 96 | + PeriodSeconds: ptr.To(int32(5)), |
| 97 | + }, |
| 98 | + }, |
| 99 | + pp: ProbePatch{Path: clearPath(), PeriodSeconds: unsetPer()}, |
| 100 | + want: func(t *testing.T, got *apps.Config) { |
| 101 | + if assert.NotNil(t, got.HealthProbe) { |
| 102 | + assert.Nil(t, got.HealthProbe.HTTPGet) |
| 103 | + if assert.NotNil(t, got.HealthProbe.PeriodSeconds) { |
| 104 | + assert.Equal(t, int32(5), *got.HealthProbe.PeriodSeconds) |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "clear PeriodSeconds sets it to nil but preserves HTTPGet", |
| 111 | + cfg: apps.Config{ |
| 112 | + HealthProbe: &apps.Probe{ |
| 113 | + ProbeHandler: apps.ProbeHandler{ |
| 114 | + HTTPGet: &apps.HTTPGetAction{Path: "/ok"}, |
| 115 | + }, |
| 116 | + PeriodSeconds: ptr.To(int32(11)), |
| 117 | + }, |
| 118 | + }, |
| 119 | + pp: ProbePatch{Path: unsetPath(), PeriodSeconds: clearPer()}, |
| 120 | + want: func(t *testing.T, got *apps.Config) { |
| 121 | + if assert.NotNil(t, got.HealthProbe) { |
| 122 | + assert.NotNil(t, got.HealthProbe.HTTPGet) |
| 123 | + assert.Equal(t, "/ok", got.HealthProbe.HTTPGet.Path) |
| 124 | + assert.Nil(t, got.HealthProbe.PeriodSeconds) |
| 125 | + } |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "clearing last fields removes the whole HealthProbe", |
| 130 | + cfg: apps.Config{ |
| 131 | + HealthProbe: &apps.Probe{ |
| 132 | + ProbeHandler: apps.ProbeHandler{HTTPGet: &apps.HTTPGetAction{Path: "/gone"}}, |
| 133 | + PeriodSeconds: nil, |
| 134 | + }, |
| 135 | + }, |
| 136 | + pp: ProbePatch{Path: clearPath(), PeriodSeconds: unsetPer()}, |
| 137 | + want: func(t *testing.T, got *apps.Config) { |
| 138 | + assert.Nil(t, got.HealthProbe) |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "unset fields do not create or modify probe", |
| 143 | + cfg: apps.Config{ |
| 144 | + HealthProbe: nil, |
| 145 | + }, |
| 146 | + pp: ProbePatch{Path: unsetPath(), PeriodSeconds: unsetPer()}, |
| 147 | + want: func(t *testing.T, got *apps.Config) { |
| 148 | + assert.Nil(t, got.HealthProbe) |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + for _, tt := range tests { |
| 154 | + t.Run(tt.name, func(t *testing.T) { |
| 155 | + cfg := tt.cfg |
| 156 | + ApplyProbePatch(&cfg, tt.pp) |
| 157 | + tt.want(t, &cfg) |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments