@@ -95,12 +95,62 @@ func TestRubyInjector_Inject(t *testing.T) {
95
95
}},
96
96
inst : current.Instrumentation {Spec : current.InstrumentationSpec {Agent : current.Agent {Language : "ruby" }, LicenseKeySecret : "newrelic-key-secret" }},
97
97
},
98
+ {
99
+ name : "a container, instrumentation, existing RUBYOPT" ,
100
+ pod : corev1.Pod {Spec : corev1.PodSpec {Containers : []corev1.Container {
101
+ {
102
+ Name : "test" ,
103
+ Env : []corev1.EnvVar {
104
+ {
105
+ Name : "RUBYOPT" ,
106
+ Value : "-r fakelib" ,
107
+ },
108
+ },
109
+ },
110
+ }}},
111
+ expectedPod : corev1.Pod {
112
+ ObjectMeta : metav1.ObjectMeta {
113
+ Labels : map [string ]string {
114
+ DescK8sAgentOperatorVersionLabelName : version .Get ().Operator ,
115
+ },
116
+ Annotations : map [string ]string {
117
+ "newrelic.com/instrumentation-versions" : `{"/":"/0"}` ,
118
+ },
119
+ }, Spec : corev1.PodSpec {
120
+ Containers : []corev1.Container {{
121
+ Name : "test" ,
122
+ Env : []corev1.EnvVar {
123
+ {Name : "RUBYOPT" , Value : "-r fakelib -r /newrelic-instrumentation/lib/boot/strap" },
124
+ {Name : "NEW_RELIC_APP_NAME" , Value : "test" },
125
+ {Name : "NEW_RELIC_LABELS" , Value : "operator:auto-injection" },
126
+ {Name : "NEW_RELIC_K8S_OPERATOR_ENABLED" , Value : "true" },
127
+ {Name : "NEW_RELIC_LICENSE_KEY" , ValueFrom : & corev1.EnvVarSource {SecretKeyRef : & corev1.SecretKeySelector {LocalObjectReference : corev1.LocalObjectReference {Name : "newrelic-key-secret" }, Key : "new_relic_license_key" , Optional : & vtrue }}},
128
+ },
129
+ VolumeMounts : []corev1.VolumeMount {{Name : "newrelic-instrumentation" , MountPath : "/newrelic-instrumentation" }},
130
+ }},
131
+ InitContainers : []corev1.Container {{
132
+ Name : "newrelic-instrumentation-ruby" ,
133
+ Command : []string {"cp" , "-a" , "/instrumentation/." , "/newrelic-instrumentation/" },
134
+ VolumeMounts : []corev1.VolumeMount {{Name : "newrelic-instrumentation" , MountPath : "/newrelic-instrumentation" }},
135
+ }},
136
+ Volumes : []corev1.Volume {{Name : "newrelic-instrumentation" , VolumeSource : corev1.VolumeSource {EmptyDir : & corev1.EmptyDirVolumeSource {}}}},
137
+ }},
138
+ inst : current.Instrumentation {Spec : current.InstrumentationSpec {Agent : current.Agent {Language : "ruby" }, LicenseKeySecret : "newrelic-key-secret" }},
139
+ },
98
140
}
99
141
for _ , test := range tests {
100
142
t .Run (test .name , func (t * testing.T ) {
101
143
ctx := context .Background ()
102
144
i := & RubyInjector {}
103
- actualPod , err := i .Inject (ctx , test .inst , test .ns , test .pod )
145
+ // inject multiple times to assert that it's idempotent
146
+ var err error
147
+ var actualPod corev1.Pod
148
+ for ic := 0 ; ic < 3 ; ic ++ {
149
+ actualPod , err = i .Inject (ctx , test .inst , test .ns , test .pod )
150
+ if err != nil {
151
+ break
152
+ }
153
+ }
104
154
errStr := ""
105
155
if err != nil {
106
156
errStr = err .Error ()
0 commit comments