@@ -2,22 +2,29 @@ package deployment
2
2
3
3
import (
4
4
"fmt"
5
+ "math/rand"
5
6
"reflect"
6
7
"sort"
7
8
"testing"
8
9
10
+ fuzz "github.com/google/gofuzz"
11
+
9
12
"k8s.io/api/core/v1"
13
+ kapiv1 "k8s.io/api/core/v1"
10
14
kerrors "k8s.io/apimachinery/pkg/api/errors"
11
15
"k8s.io/apimachinery/pkg/api/resource"
16
+ "k8s.io/apimachinery/pkg/api/testing/fuzzer"
12
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
18
"k8s.io/apimachinery/pkg/runtime"
19
+ "k8s.io/apimachinery/pkg/util/diff"
14
20
kinformers "k8s.io/client-go/informers"
15
21
kclientset "k8s.io/client-go/kubernetes"
16
22
"k8s.io/client-go/kubernetes/fake"
17
23
clientgotesting "k8s.io/client-go/testing"
18
24
"k8s.io/client-go/tools/cache"
19
25
kapi "k8s.io/kubernetes/pkg/api"
20
26
kapihelper "k8s.io/kubernetes/pkg/api/helper"
27
+ kapitesting "k8s.io/kubernetes/pkg/api/testing"
21
28
22
29
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
23
30
_ "github.com/openshift/origin/pkg/apps/apis/apps/install"
@@ -1075,3 +1082,76 @@ func TestDeployerCustomLabelsAndAnnotations(t *testing.T) {
1075
1082
expectMapContains (t , podTemplate .Annotations , test .annotations , "annotations" )
1076
1083
}
1077
1084
}
1085
+
1086
+ func TestMakeDeployerPod (t * testing.T ) {
1087
+ client := & fake.Clientset {}
1088
+ controller := okDeploymentController (client , nil , nil , true , v1 .PodUnknown )
1089
+ config := deploytest .OkDeploymentConfig (1 )
1090
+ deployment , _ := deployutil .MakeDeploymentV1 (config , codec )
1091
+ container := controller .makeDeployerContainer (& config .Spec .Strategy )
1092
+ container .Resources = deployutil .CopyApiResourcesToV1Resources (& config .Spec .Strategy .Resources )
1093
+ defaultGracePeriod := int64 (10 )
1094
+ maxDeploymentDurationSeconds := deployapi .MaxDeploymentDurationSeconds
1095
+
1096
+ for i := 1 ; i <= 25 ; i ++ {
1097
+ seed := rand .Int63 ()
1098
+ f := fuzzer .FuzzerFor (kapitesting .FuzzerFuncs , rand .NewSource (seed ), kapi .Codecs )
1099
+ f .Funcs (
1100
+ func (p * kapiv1.PodTemplateSpec , c fuzz.Continue ) {
1101
+ c .FuzzNoCustom (p )
1102
+ p .Spec .InitContainers = nil
1103
+
1104
+ // These are specific for deployer pod container:
1105
+ p .Spec .Containers = []kapiv1.Container {* container }
1106
+ p .Spec .Containers [0 ].Name = "deployment"
1107
+ p .Spec .Containers [0 ].Command = container .Command
1108
+ p .Spec .Containers [0 ].Args = container .Args
1109
+ p .Spec .Containers [0 ].Image = container .Image
1110
+ p .Spec .Containers [0 ].Env = append (p .Spec .Containers [0 ].Env , kapiv1.EnvVar {Name : "OPENSHIFT_DEPLOYMENT_NAME" , Value : "config-1" })
1111
+ p .Spec .Containers [0 ].Env = append (p .Spec .Containers [0 ].Env , kapiv1.EnvVar {Name : "OPENSHIFT_DEPLOYMENT_NAMESPACE" , Value : "default" })
1112
+ p .Spec .Containers [0 ].Resources = container .Resources
1113
+ p .Spec .Containers [0 ].ImagePullPolicy = kapiv1 .PullIfNotPresent
1114
+
1115
+ // These are hardcoded for deployer pod spec
1116
+ p .Spec .RestartPolicy = kapiv1 .RestartPolicyNever
1117
+ p .Spec .TerminationGracePeriodSeconds = & defaultGracePeriod
1118
+ p .Spec .ActiveDeadlineSeconds = & maxDeploymentDurationSeconds
1119
+ p .Spec .ServiceAccountName = "sa:test"
1120
+
1121
+ // FIXME: These are weird or missing. If you get an error below, consider
1122
+ // adding this field into deployer controller or to this list:
1123
+ p .Spec .DeprecatedServiceAccount = ""
1124
+ p .Spec .AutomountServiceAccountToken = nil
1125
+ p .Spec .Tolerations = nil
1126
+ p .Spec .Volumes = nil
1127
+ p .Spec .NodeName = ""
1128
+ p .Spec .HostNetwork = false
1129
+ p .Spec .HostPID = false
1130
+ p .Spec .HostIPC = false
1131
+ p .Spec .Hostname = ""
1132
+ p .Spec .Subdomain = ""
1133
+ p .Spec .Affinity = nil
1134
+ p .Spec .SchedulerName = ""
1135
+ p .Spec .HostAliases = nil
1136
+ p .Spec .Priority = nil
1137
+ p .Spec .PriorityClassName = ""
1138
+ p .Spec .SecurityContext = nil
1139
+ },
1140
+ )
1141
+ inputPodTemplate := & kapiv1.PodTemplateSpec {}
1142
+ f .Fuzz (& inputPodTemplate )
1143
+ deployment .Spec .Template = inputPodTemplate
1144
+
1145
+ outputPodTemplate , err := controller .makeDeployerPod (deployment )
1146
+ if err != nil {
1147
+ t .Fatalf ("unexpected error: %v" , err )
1148
+ }
1149
+
1150
+ if ! reflect .DeepEqual (inputPodTemplate .Spec , outputPodTemplate .Spec ) {
1151
+ t .Fatalf ("Deployer pod is missing fields:\n %s\n \n %s" ,
1152
+ diff .ObjectReflectDiff (inputPodTemplate .Spec , outputPodTemplate .Spec ),
1153
+ diff .ObjectDiff (inputPodTemplate .Spec , outputPodTemplate .Spec ),
1154
+ )
1155
+ }
1156
+ }
1157
+ }
0 commit comments