k8s-deploy/src/strategyHelpers/blueGreen/smiBlueGreenHelper.test.ts
Jaiveer Katariya e9693a7cdd
Added Integration Tests, Resolved Bugs With Annotations (#255)
* First commit - made manifests for test deployments, made manifests for i tests for other deployment strategies

* broke down blue/green

* added latest tags to test manifests for new tags

* remade tester

* ready to test bgi

* using all but first index of argv

* careless error with dicts

* added test to namespace

* realized i was silencing error

* indexing containers

* keyerror

* logging bc python errors are weird

* expected still string

* parsed args behaving weirdly

* test seems to be working now, applying changes to other YAMLs now

* blue/green ready to test

* oops

* oops

* Added additional labels to check

* hyphen

* Added our annotations

* lol

* added our labels to services too

* nonetype issue'

* nonetype issue'

* narrowing down parameter

* fixed annotations issue with promote

* adding debhug statement to figure out why services aren't getting annotations

* this should fix annotations issue for service

* not sure why this wasn't caught by intellisense

* should be fixed with removing comma but adding logs in case

* added linkerd install

* verification

* upgraded kubernetes version

* removing crds

* proxy option

* Added smi extension

* logging service

* smi svcs also getting labeled now

* matching ts type

* not sure where stable service is going

* remaining svc and deployment should match

* keeping stable service and ts object

* updated tests to reflect keeping ts object

* no green svc after promote

* duh

* lol

* canary work

* canary test ready

* logging for ing, filename for canary

* changed ingress svc key and returning svc files from smi canary deployment

* ts name

* forgot about baseline in first deploy

* *

* *

* smi canary should annotate, fixed cleanup

* typescript issue plus percentage

* forgot to type extra method

* removed cleaned up objects from annotate list

* logging because services aren't getting removed

* moving to try/catch strategy of annotation since deletion can fail silently/with warnings

* moved label to individual

* removing canary service check after promote

* pod ready for testing

* set weights to 1000

* selectors

* *

* percentage

* *

* typing

* mixed up pod and smi

* fixed tests

* prettier

* forgot to remove canary

* cleanup

* Added oliver's feedback + more cleanup

* ncc as dev dependency

* npx

* going back to global ncc install bc npm is being weird

* prettier

* removed unnecessary post step
2022-11-01 16:02:57 -04:00

201 lines
6.0 KiB
TypeScript

import * as core from '@actions/core'
import {TrafficSplitObject} from '../../types/k8sObject'
import {Kubectl} from '../../types/kubectl'
import * as fileHelper from '../../utilities/fileUtils'
import * as TSutils from '../../utilities/trafficSplitUtils'
import {BlueGreenManifests} from '../../types/blueGreenTypes'
import {
BLUE_GREEN_VERSION_LABEL,
getManifestObjects,
GREEN_LABEL_VALUE,
NONE_LABEL_VALUE
} from './blueGreenHelper'
import {
cleanupSMI,
createTrafficSplitObject,
getGreenSMIServiceResource,
getStableSMIServiceResource,
MAX_VAL,
MIN_VAL,
setupSMI,
TRAFFIC_SPLIT_OBJECT,
TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX,
validateTrafficSplitsState
} from './smiBlueGreenHelper'
import * as bgHelper from './blueGreenHelper'
jest.mock('../../types/kubectl')
const kc = new Kubectl('')
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
const mockTsObject: TrafficSplitObject = {
apiVersion: 'v1alpha3',
kind: TRAFFIC_SPLIT_OBJECT,
metadata: {
name: 'nginx-service-trafficsplit',
labels: new Map<string, string>(),
annotations: new Map<string, string>()
},
spec: {
service: 'nginx-service',
backends: [
{
service: 'nginx-service-stable',
weight: MIN_VAL
},
{
service: 'nginx-service-green',
weight: MAX_VAL
}
]
}
}
describe('SMI Helper tests', () => {
let testObjects: BlueGreenManifests
beforeEach(() => {
//@ts-ignore
Kubectl.mockClear()
jest
.spyOn(TSutils, 'getTrafficSplitAPIVersion')
.mockImplementation(() => Promise.resolve(''))
testObjects = getManifestObjects(ingressFilepath)
jest
.spyOn(fileHelper, 'writeObjectsToFile')
.mockImplementationOnce(() => [''])
})
test('setupSMI tests', async () => {
const smiResults = await setupSMI(kc, testObjects.serviceEntityList)
let found = 0
for (const obj of smiResults.objects) {
if (obj.metadata.name === 'nginx-service-stable') {
expect(obj.metadata.labels[BLUE_GREEN_VERSION_LABEL]).toBe(
NONE_LABEL_VALUE
)
expect(obj.spec.selector.app).toBe('nginx')
found++
}
if (obj.metadata.name === 'nginx-service-green') {
expect(obj.metadata.labels[BLUE_GREEN_VERSION_LABEL]).toBe(
GREEN_LABEL_VALUE
)
found++
}
if (obj.metadata.name === 'nginx-service-trafficsplit') {
found++
// expect stable weight to be max val
const casted = obj as TrafficSplitObject
expect(casted.spec.backends).toHaveLength(2)
for (const be of casted.spec.backends) {
if (be.service === 'nginx-service-stable') {
expect(be.weight).toBe(MAX_VAL)
}
if (be.service === 'nginx-service-green') {
expect(be.weight).toBe(MIN_VAL)
}
}
}
}
expect(found).toBe(3)
})
test('createTrafficSplitObject tests', async () => {
const noneTsObject: TrafficSplitObject = await createTrafficSplitObject(
kc,
testObjects.serviceEntityList[0].metadata.name,
NONE_LABEL_VALUE
)
expect(noneTsObject.metadata.name).toBe('nginx-service-trafficsplit')
for (let be of noneTsObject.spec.backends) {
if (be.service === 'nginx-service-stable') {
expect(be.weight).toBe(MAX_VAL)
}
if (be.service === 'nginx-service-green') {
expect(be.weight).toBe(MIN_VAL)
}
}
const greenTsObject: TrafficSplitObject = await createTrafficSplitObject(
kc,
testObjects.serviceEntityList[0].metadata.name,
GREEN_LABEL_VALUE
)
expect(greenTsObject.metadata.name).toBe('nginx-service-trafficsplit')
for (const be of greenTsObject.spec.backends) {
if (be.service === 'nginx-service-stable') {
expect(be.weight).toBe(MIN_VAL)
}
if (be.service === 'nginx-service-green') {
expect(be.weight).toBe(MAX_VAL)
}
}
})
test('getSMIServiceResource test', () => {
const stableResult = getStableSMIServiceResource(
testObjects.serviceEntityList[0]
)
const greenResult = getGreenSMIServiceResource(
testObjects.serviceEntityList[0]
)
expect(stableResult.metadata.name).toBe('nginx-service-stable')
expect(stableResult.metadata.labels[BLUE_GREEN_VERSION_LABEL]).toBe(
NONE_LABEL_VALUE
)
expect(greenResult.metadata.name).toBe('nginx-service-green')
expect(greenResult.metadata.labels[BLUE_GREEN_VERSION_LABEL]).toBe(
GREEN_LABEL_VALUE
)
})
test('validateTrafficSplitsState', async () => {
jest
.spyOn(bgHelper, 'fetchResource')
.mockImplementation(() => Promise.resolve(mockTsObject))
let valResult = await validateTrafficSplitsState(
kc,
testObjects.serviceEntityList
)
expect(valResult).toBe(true)
const mockTsCopy = JSON.parse(JSON.stringify(mockTsObject))
mockTsCopy.spec.backends[0].weight = MAX_VAL
jest
.spyOn(bgHelper, 'fetchResource')
.mockImplementation(() => Promise.resolve(mockTsCopy))
valResult = await validateTrafficSplitsState(
kc,
testObjects.serviceEntityList
)
expect(valResult).toBe(false)
jest.spyOn(bgHelper, 'fetchResource').mockImplementation()
valResult = await validateTrafficSplitsState(
kc,
testObjects.serviceEntityList
)
expect(valResult).toBe(false)
})
test('cleanupSMI test', async () => {
const deleteObjects = await cleanupSMI(kc, testObjects.serviceEntityList)
expect(deleteObjects).toHaveLength(1)
expect(deleteObjects[0].name).toBe('nginx-service-green')
expect(deleteObjects[0].kind).toBe('Service')
})
})