Skip to content

Commit bf517cc

Browse files
committed
fix: get correct versions in the session
1 parent 1e577d1 commit bf517cc

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

src/otomi-stack.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,97 @@ describe('Users tests', () => {
753753
})
754754
})
755755

756+
describe('getVersions', () => {
757+
let otomiStack: OtomiStack
758+
759+
beforeEach(async () => {
760+
otomiStack = new OtomiStack()
761+
await otomiStack.init()
762+
})
763+
764+
afterEach(() => {
765+
jest.restoreAllMocks()
766+
})
767+
768+
test('should return versions with otomi version from settings', () => {
769+
const mockSettings = { otomi: { version: '1.2.3' } }
770+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue(mockSettings)
771+
772+
const result = (otomiStack as any).getVersions('abc123')
773+
774+
expect(result).toHaveProperty('core', '1.2.3')
775+
expect(result).toHaveProperty('api')
776+
expect(result).toHaveProperty('console')
777+
expect(result).toHaveProperty('values', 'abc123')
778+
expect(otomiStack.getSettings).toHaveBeenCalledWith(['otomi'])
779+
})
780+
781+
test('should fallback to env.VERSIONS.core when otomi.version is not available', () => {
782+
const mockSettings = { otomi: undefined }
783+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue(mockSettings)
784+
785+
const result = (otomiStack as any).getVersions('def456')
786+
787+
expect(result).toHaveProperty('core')
788+
expect(result).toHaveProperty('api')
789+
expect(result).toHaveProperty('console')
790+
expect(result).toHaveProperty('values', 'def456')
791+
})
792+
793+
test('should fallback to process.env.npm_package_version when env.VERSIONS.api is not available', () => {
794+
const originalNpmVersion = process.env.npm_package_version
795+
process.env.npm_package_version = '5.0.0'
796+
797+
const mockSettings = { otomi: { version: '1.2.3' } }
798+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue(mockSettings)
799+
800+
const result = (otomiStack as any).getVersions('ghi789')
801+
802+
expect(result).toHaveProperty('core', '1.2.3')
803+
expect(result).toHaveProperty('api')
804+
expect(result).toHaveProperty('console')
805+
expect(result).toHaveProperty('values', 'ghi789')
806+
807+
process.env.npm_package_version = originalNpmVersion
808+
})
809+
810+
test('should handle undefined otomi settings gracefully', () => {
811+
const mockSettings = {}
812+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue(mockSettings)
813+
814+
const result = (otomiStack as any).getVersions('xyz123')
815+
816+
expect(result).toHaveProperty('core')
817+
expect(result).toHaveProperty('api')
818+
expect(result).toHaveProperty('console')
819+
expect(result).toHaveProperty('values', 'xyz123')
820+
})
821+
822+
test('should pass through currentSha as values field', () => {
823+
const mockSettings = { otomi: { version: '1.0.0' } }
824+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue(mockSettings)
825+
826+
const testSha = 'unique-commit-sha-123'
827+
const result = (otomiStack as any).getVersions(testSha)
828+
829+
expect(result.values).toBe(testSha)
830+
expect(typeof result.values).toBe('string')
831+
})
832+
833+
test('should return all required version fields', () => {
834+
const mockSettings = { otomi: { version: '1.0.0' } }
835+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue(mockSettings)
836+
837+
const result = (otomiStack as any).getVersions('test-sha')
838+
839+
expect(Object.keys(result).sort()).toEqual(['api', 'console', 'core', 'values'])
840+
expect(typeof result.core).toBe('string')
841+
expect(typeof result.api).toBe('string')
842+
expect(typeof result.console).toBe('string')
843+
expect(typeof result.values).toBe('string')
844+
})
845+
})
846+
756847
describe('PodService', () => {
757848
let otomiStack: OtomiStack
758849
let clientMock: {

src/otomi-stack.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,6 @@ export default class OtomiStack {
530530
}, {})
531531
updatedSettingsData.otomi.nodeSelector = nodeSelectorObject
532532
}
533-
// Update environment VERSIONS.core when otomi version changes
534-
if (updatedSettingsData.otomi?.version && updatedSettingsData.otomi?.version !== env.VERSIONS.core) {
535-
env.VERSIONS.core = updatedSettingsData.otomi.version
536-
}
537533
}
538534

539535
settings[settingId] = removeBlankAttributes(updatedSettingsData[settingId] as Record<string, any>)
@@ -2442,6 +2438,16 @@ export default class OtomiStack {
24422438
}
24432439
}
24442440

2441+
private getVersions(currentSha: string): Record<string, string> {
2442+
const { otomi } = this.getSettings(['otomi'])
2443+
return {
2444+
core: otomi?.version ?? env.VERSIONS.core,
2445+
api: env.VERSIONS.api ?? process.env.npm_package_version,
2446+
console: env.VERSIONS.console,
2447+
values: currentSha,
2448+
}
2449+
}
2450+
24452451
async getSession(user: k8sUser): Promise<Session> {
24462452
const rootStack = await getSessionStack()
24472453
const valuesSchema = await getValuesSchema()
@@ -2472,12 +2478,7 @@ export default class OtomiStack {
24722478
objStorageApps: env.OBJ_STORAGE_APPS,
24732479
objStorageRegions,
24742480
},
2475-
versions: {
2476-
core: env.VERSIONS.core,
2477-
api: env.VERSIONS.api ?? process.env.npm_package_version,
2478-
console: env.VERSIONS.console,
2479-
values: currentSha,
2480-
},
2481+
versions: this.getVersions(currentSha),
24812482
valuesSchema,
24822483
}
24832484
return data

0 commit comments

Comments
 (0)