github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/status_test.go (about) 1 // +build unit 2 3 package cmd_test 4 5 import ( 6 "os" 7 "testing" 8 9 "github.com/olli-ai/jx/v2/pkg/cmd" 10 cmd_mocks "github.com/olli-ai/jx/v2/pkg/cmd/clients/mocks" 11 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 12 13 . "github.com/petergtz/pegomock" 14 "github.com/stretchr/testify/assert" 15 appsv1 "k8s.io/api/apps/v1" 16 v1 "k8s.io/api/core/v1" 17 "k8s.io/apimachinery/pkg/api/resource" 18 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 kube_mocks "k8s.io/client-go/kubernetes/fake" 20 ) 21 22 func TestStatusRun(t *testing.T) { 23 // Create a fake node 24 node := &v1.Node{ 25 ObjectMeta: meta_v1.ObjectMeta{ 26 Name: "test-node-1", 27 }, 28 Status: v1.NodeStatus{ 29 Capacity: v1.ResourceList{ 30 v1.ResourceCPU: resource.MustParse("10"), 31 v1.ResourceMemory: resource.MustParse("10G"), 32 }, 33 }, 34 } 35 36 replicaCount := int32(1) 37 38 labels := make(map[string]string) 39 labels["app"] = "ians-app" 40 41 // Create a fake Jenkins deployment 42 deployment := &appsv1.Deployment{ 43 ObjectMeta: meta_v1.ObjectMeta{ 44 Name: "jenkins", 45 Namespace: "jx-testing", 46 }, 47 Spec: appsv1.DeploymentSpec{ 48 Replicas: &replicaCount, 49 Selector: &meta_v1.LabelSelector{ 50 MatchLabels: map[string]string{ 51 "app": "ians-app", 52 }, 53 }, 54 Template: v1.PodTemplateSpec{ 55 ObjectMeta: meta_v1.ObjectMeta{ 56 Labels: map[string]string{ 57 "app": "ians-app", 58 }, 59 }, 60 Spec: v1.PodSpec{ 61 Containers: []v1.Container{ 62 { 63 Name: "web", 64 Image: "nginx:1.12", 65 Ports: []v1.ContainerPort{ 66 { 67 Name: "http", 68 Protocol: v1.ProtocolTCP, 69 ContainerPort: 80, 70 }, 71 }, 72 }, 73 }, 74 }, 75 }, 76 }, 77 Status: appsv1.DeploymentStatus{ 78 Replicas: 1, 79 UpdatedReplicas: 1, 80 AvailableReplicas: 1, 81 UnavailableReplicas: 0, 82 ReadyReplicas: 1, 83 CollisionCount: nil, 84 }, 85 } 86 87 annotations := make(map[string]string) 88 annotations["fabric8.io/exposeUrl"] = "http://jenkins.testorama.com" 89 90 // Create a fake Jenkins service 91 service := &v1.Service{ 92 ObjectMeta: meta_v1.ObjectMeta{ 93 Name: "jenkins", 94 Namespace: "jx-testing", 95 Annotations: annotations, 96 }, 97 } 98 99 // mock factory 100 factory := cmd_mocks.NewMockFactory() 101 // mock Kubernetes interface 102 kubernetesInterface := kube_mocks.NewSimpleClientset(node, deployment, service) 103 // Override CreateKubeClient to return mock Kubernetes interface 104 When(factory.CreateKubeClient()).ThenReturn(kubernetesInterface, "jx-testing", nil) 105 106 // Setup options 107 commonOpts := opts.NewCommonOptionsWithFactory(factory) 108 commonOpts.Out = os.Stdout 109 commonOpts.Err = os.Stderr 110 options := &cmd.StatusOptions{ 111 CommonOptions: &commonOpts, 112 } 113 114 err := options.Run() 115 116 assert.NoError(t, err, "Should not error") 117 118 }