github.com/kotalco/kotal@v0.3.0/controllers/ipfs/peer_controller_test.go (about) 1 package controllers 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "time" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 "github.com/onsi/gomega/gstruct" 12 13 ipfsv1alpha1 "github.com/kotalco/kotal/apis/ipfs/v1alpha1" 14 "github.com/kotalco/kotal/controllers/shared" 15 16 appsv1 "k8s.io/api/apps/v1" 17 corev1 "k8s.io/api/core/v1" 18 "k8s.io/apimachinery/pkg/api/resource" 19 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 20 "k8s.io/apimachinery/pkg/types" 21 "k8s.io/apimachinery/pkg/util/intstr" 22 ) 23 24 var _ = Describe("IPFS peer controller", func() { 25 ns := &corev1.Namespace{ 26 ObjectMeta: metav1.ObjectMeta{ 27 Name: "ipfs-peer", 28 }, 29 } 30 31 key := types.NamespacedName{ 32 Name: "my-peer", 33 Namespace: ns.Name, 34 } 35 36 image := "kotalco/kubo:test" 37 38 spec := ipfsv1alpha1.PeerSpec{ 39 Image: image, 40 API: true, 41 APIPort: 3333, 42 Gateway: true, 43 GatewayPort: 4444, 44 Routing: ipfsv1alpha1.DHTClientRouting, 45 SwarmKeySecretName: "my-swarm", 46 } 47 48 toCreate := &ipfsv1alpha1.Peer{ 49 ObjectMeta: metav1.ObjectMeta{ 50 Name: key.Name, 51 Namespace: key.Namespace, 52 }, 53 Spec: spec, 54 } 55 56 t := true 57 58 peerOwnerReference := metav1.OwnerReference{ 59 APIVersion: "ipfs.kotal.io/v1alpha1", 60 Kind: "Peer", 61 Name: toCreate.Name, 62 Controller: &t, 63 BlockOwnerDeletion: &t, 64 } 65 66 It(fmt.Sprintf("Should create %s namespace", ns.Name), func() { 67 Expect(k8sClient.Create(context.TODO(), ns)).To(Succeed()) 68 }) 69 70 It("should create ipfs peer", func() { 71 if os.Getenv(shared.EnvUseExistingCluster) != "true" { 72 toCreate.Default() 73 } 74 Expect(k8sClient.Create(context.Background(), toCreate)).Should(Succeed()) 75 }) 76 77 It("Should get ipfs peer", func() { 78 fetched := &ipfsv1alpha1.Peer{} 79 Expect(k8sClient.Get(context.Background(), key, fetched)).To(Succeed()) 80 Expect(fetched.Spec).To(Equal(toCreate.Spec)) 81 peerOwnerReference.UID = fetched.UID 82 time.Sleep(5 * time.Second) 83 }) 84 85 It("Should create peer statefulset with correct arguments", func() { 86 fetched := &appsv1.StatefulSet{} 87 Expect(k8sClient.Get(context.Background(), key, fetched)).To(Succeed()) 88 Expect(fetched.OwnerReferences).To(ContainElements(peerOwnerReference)) 89 Expect(*fetched.Spec.Template.Spec.SecurityContext).To(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{ 90 "RunAsUser": gstruct.PointTo(Equal(int64(1000))), 91 "RunAsGroup": gstruct.PointTo(Equal(int64(3000))), 92 "FSGroup": gstruct.PointTo(Equal(int64(2000))), 93 "RunAsNonRoot": gstruct.PointTo(Equal(true)), 94 })) 95 Expect(fetched.Spec.Template.Spec.Containers[0].Image).To(Equal(image)) 96 }) 97 98 It("Should create allocate correct resources to peer statefulset", func() { 99 fetched := &appsv1.StatefulSet{} 100 expectedResources := corev1.ResourceRequirements{ 101 Requests: corev1.ResourceList{ 102 corev1.ResourceCPU: resource.MustParse(ipfsv1alpha1.DefaultNodeCPURequest), 103 corev1.ResourceMemory: resource.MustParse(ipfsv1alpha1.DefaultNodeMemoryRequest), 104 }, 105 Limits: corev1.ResourceList{ 106 corev1.ResourceCPU: resource.MustParse(ipfsv1alpha1.DefaultNodeCPULimit), 107 corev1.ResourceMemory: resource.MustParse(ipfsv1alpha1.DefaultNodeMemoryLimit), 108 }, 109 } 110 Expect(k8sClient.Get(context.Background(), key, fetched)).To(Succeed()) 111 Expect(fetched.Spec.Template.Spec.Containers[0].Resources).To(Equal(expectedResources)) 112 }) 113 114 It("Should create peer configmap", func() { 115 fetched := &corev1.ConfigMap{} 116 Expect(k8sClient.Get(context.Background(), key, fetched)).To(Succeed()) 117 Expect(fetched.OwnerReferences).To(ContainElements(peerOwnerReference)) 118 Expect(fetched.Data).To(HaveKey("init_ipfs_config.sh")) 119 Expect(fetched.Data).To(HaveKey("config_ipfs.sh")) 120 Expect(fetched.Data).To(HaveKey("copy_swarm_key.sh")) 121 }) 122 123 It("Should create peer data persistent volume with correct resources", func() { 124 fetched := &corev1.PersistentVolumeClaim{} 125 Expect(k8sClient.Get(context.Background(), key, fetched)).To(Succeed()) 126 Expect(fetched.OwnerReferences).To(ContainElements(peerOwnerReference)) 127 expectedResources := corev1.VolumeResourceRequirements{ 128 Requests: corev1.ResourceList{ 129 corev1.ResourceStorage: resource.MustParse(ipfsv1alpha1.DefaultNodeStorageRequest), 130 }, 131 } 132 Expect(fetched.Spec.Resources).To(Equal(expectedResources)) 133 }) 134 135 It("Should create peer service", func() { 136 fetched := &corev1.Service{} 137 Expect(k8sClient.Get(context.Background(), key, fetched)).To(Succeed()) 138 Expect(fetched.OwnerReferences).To(ContainElements(peerOwnerReference)) 139 Expect(fetched.Spec.Ports).To(ContainElements( 140 []corev1.ServicePort{ 141 { 142 Name: "swarm", 143 Port: 4001, 144 TargetPort: intstr.FromString("swarm"), 145 Protocol: corev1.ProtocolTCP, 146 }, 147 { 148 Name: "swarm-udp", 149 Port: 4001, 150 TargetPort: intstr.FromString("swarm-udp"), 151 Protocol: corev1.ProtocolUDP, 152 }, 153 { 154 Name: "api", 155 Port: int32(3333), 156 TargetPort: intstr.FromString("api"), 157 Protocol: corev1.ProtocolTCP, 158 }, 159 { 160 Name: "gateway", 161 Port: int32(4444), 162 TargetPort: intstr.FromString("gateway"), 163 Protocol: corev1.ProtocolTCP, 164 }, 165 }, 166 )) 167 }) 168 169 It(fmt.Sprintf("Should delete %s namespace", ns.Name), func() { 170 Expect(k8sClient.Delete(context.Background(), ns)).To(Succeed()) 171 }) 172 173 })