deployment_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package versioned
  14. import (
  15. "reflect"
  16. "testing"
  17. appsv1 "k8s.io/api/apps/v1"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. )
  21. func TestDeploymentBasicGenerate(t *testing.T) {
  22. one := int32(1)
  23. tests := []struct {
  24. name string
  25. deploymentName string
  26. images []string
  27. expected *appsv1.Deployment
  28. expectErr bool
  29. }{
  30. {
  31. name: "deployment name and images ok",
  32. deploymentName: "images-name-ok",
  33. images: []string{"nn/image1", "registry/nn/image2", "nn/image3:tag", "nn/image4@digest", "nn/image5@sha256:digest"},
  34. expected: &appsv1.Deployment{
  35. ObjectMeta: metav1.ObjectMeta{
  36. Name: "images-name-ok",
  37. Labels: map[string]string{"app": "images-name-ok"},
  38. },
  39. Spec: appsv1.DeploymentSpec{
  40. Replicas: &one,
  41. Selector: &metav1.LabelSelector{
  42. MatchLabels: map[string]string{"app": "images-name-ok"},
  43. },
  44. Template: v1.PodTemplateSpec{
  45. ObjectMeta: metav1.ObjectMeta{
  46. Labels: map[string]string{"app": "images-name-ok"},
  47. },
  48. Spec: v1.PodSpec{
  49. Containers: []v1.Container{
  50. {Name: "image1", Image: "nn/image1"},
  51. {Name: "image2", Image: "registry/nn/image2"},
  52. {Name: "image3", Image: "nn/image3:tag"},
  53. {Name: "image4", Image: "nn/image4@digest"},
  54. {Name: "image5", Image: "nn/image5@sha256:digest"},
  55. },
  56. },
  57. },
  58. },
  59. },
  60. expectErr: false,
  61. },
  62. {
  63. name: "empty images",
  64. deploymentName: "images-empty",
  65. images: []string{},
  66. expectErr: true,
  67. },
  68. {
  69. name: "no images",
  70. deploymentName: "images-missing",
  71. expectErr: true,
  72. },
  73. {
  74. name: "no deployment name and images",
  75. expectErr: true,
  76. },
  77. }
  78. for _, tt := range tests {
  79. t.Run(tt.name, func(t *testing.T) {
  80. generator := &DeploymentBasicAppsGeneratorV1{
  81. BaseDeploymentGenerator{
  82. Name: tt.deploymentName,
  83. Images: tt.images,
  84. },
  85. }
  86. obj, err := generator.StructuredGenerate()
  87. if !tt.expectErr && err != nil {
  88. t.Errorf("unexpected error: %v", err)
  89. }
  90. if tt.expectErr && err != nil {
  91. return
  92. }
  93. if !reflect.DeepEqual(obj.(*appsv1.Deployment), tt.expected) {
  94. t.Errorf("test: %v\nexpected:\n%#v\nsaw:\n%#v", tt.name, tt.expected, obj.(*appsv1.Deployment))
  95. }
  96. })
  97. }
  98. }