(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func TestAddToPod(t *testing.T) { |
| 21 | // setupContext creates a context with the given feature gates enabled. |
| 22 | setupContext := func(logs, metrics bool) context.Context { |
| 23 | gate := feature.NewGate() |
| 24 | assert.NilError(t, gate.SetFromMap(map[string]bool{ |
| 25 | feature.OpenTelemetryLogs: logs, |
| 26 | feature.OpenTelemetryMetrics: metrics, |
| 27 | })) |
| 28 | return feature.NewContext(context.Background(), gate) |
| 29 | } |
| 30 | |
| 31 | // newPodTemplate returns a minimal PodTemplateSpec for testing. |
| 32 | newPodTemplate := func() *corev1.PodTemplateSpec { |
| 33 | return &corev1.PodTemplateSpec{} |
| 34 | } |
| 35 | |
| 36 | // newConfigMap returns a ConfigMap with a name, as used by callers. |
| 37 | newConfigMap := func() *corev1.ConfigMap { |
| 38 | cm := &corev1.ConfigMap{} |
| 39 | cm.Name = "test-configmap" |
| 40 | return cm |
| 41 | } |
| 42 | |
| 43 | // findContainer returns the collector container from the template, if present. |
| 44 | findContainer := func(template *corev1.PodTemplateSpec) *corev1.Container { |
| 45 | for i := range template.Spec.Containers { |
| 46 | if template.Spec.Containers[i].Name == naming.ContainerCollector { |
| 47 | return &template.Spec.Containers[i] |
| 48 | } |
| 49 | } |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | // findVolume returns the named volume from the template, if present. |
| 54 | findVolume := func(template *corev1.PodTemplateSpec, name string) *corev1.Volume { |
| 55 | for i := range template.Spec.Volumes { |
| 56 | if template.Spec.Volumes[i].Name == name { |
| 57 | return &template.Spec.Volumes[i] |
| 58 | } |
| 59 | } |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | t.Run("NoOpWhenFeaturesDisabled", func(t *testing.T) { |
| 64 | ctx := setupContext(false, false) |
| 65 | spec := &v1beta1.InstrumentationSpec{} |
| 66 | template := newPodTemplate() |
| 67 | |
| 68 | AddToPod(ctx, spec, corev1.PullIfNotPresent, newConfigMap(), |
| 69 | template, nil, "", nil, false, false) |
| 70 | |
| 71 | assert.Assert(t, cmp.Len(template.Spec.Containers, 0), |
| 72 | "expected no containers when features are disabled") |
| 73 | assert.Assert(t, cmp.Len(template.Spec.Volumes, 0), |
| 74 | "expected no volumes when features are disabled") |
| 75 | }) |
| 76 | |
| 77 | t.Run("NoOpWhenSpecIsNil", func(t *testing.T) { |
nothing calls this directly
no test coverage detected