11const {
22 checkInstaInfraExists,
3- runServices
3+ runServices,
4+ shutdownServices
45} = require ( '../../src/util/insta-infra' )
5- const fs = require ( 'fs' )
66const { execSync } = require ( 'child_process' )
77const logger = require ( '../../src/util/log' )
88const { isContainerFinished } = require ( '../../src/util/docker' )
@@ -12,69 +12,65 @@ jest.mock('../../src/util/log')
1212jest . mock ( '../../src/util/docker' )
1313
1414describe ( 'checkInstaInfraExists' , ( ) => {
15- it ( 'should clone repository if insta-infra folder does not exist' , ( ) => {
16- jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( false )
17- execSync . mockReturnValueOnce ( 'success' )
18- checkInstaInfraExists ( '/path/to/insta-infra' )
19- expect ( execSync ) . toHaveBeenCalledWith (
20- 'git clone git@github.com:data-catering/insta-infra.git /path/to/insta-infra'
21- )
15+ beforeEach ( ( ) => {
16+ jest . resetAllMocks ( )
17+ } )
18+
19+ it ( 'should not install if insta CLI is already installed' , ( ) => {
20+ execSync . mockReturnValueOnce ( '/usr/local/bin/insta' )
21+ checkInstaInfraExists ( )
22+ expect ( execSync ) . toHaveBeenCalledWith ( 'which insta' , {
23+ encoding : 'utf-8' ,
24+ stdio : 'pipe'
25+ } )
26+ expect ( execSync ) . toHaveBeenCalledTimes ( 1 )
2227 } )
2328
24- it ( 'should log error and retry with https if git clone via ssh fails' , ( ) => {
25- jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( false )
29+ it ( 'should install insta CLI if not found' , ( ) => {
2630 execSync . mockImplementationOnce ( ( ) => {
27- throw new Error ( 'ssh failed ' )
31+ throw new Error ( 'not found ' )
2832 } )
2933 execSync . mockReturnValueOnce ( 'success' )
30- checkInstaInfraExists ( '/path/to/insta-infra' )
34+ checkInstaInfraExists ( )
3135 expect ( execSync ) . toHaveBeenCalledWith (
32- 'git clone https://github.com/data-catering/insta-infra.git /path/to/insta-infra'
36+ 'curl -fsSL https://raw.githubusercontent.com/data-catering/insta-infra/main/install.sh | sh' ,
37+ { stdio : 'pipe' }
3338 )
3439 } )
3540
36- it ( 'should throw error if both git clone via ssh and https fail' , ( ) => {
37- jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( false )
41+ it ( 'should throw error if installation fails' , ( ) => {
3842 execSync . mockImplementationOnce ( ( ) => {
39- throw new Error ( 'ssh failed ' )
43+ throw new Error ( 'not found ' )
4044 } )
4145 execSync . mockImplementationOnce ( ( ) => {
42- throw new Error ( 'https failed' )
46+ throw new Error ( 'install failed' )
4347 } )
44- expect ( ( ) => checkInstaInfraExists ( '/path/to/insta-infra' ) ) . toThrow (
45- 'Failed to checkout insta- infra repository '
48+ expect ( ( ) => checkInstaInfraExists ( ) ) . toThrow (
49+ 'Failed to install insta CLI. Please install manually: https://github.com/data-catering/insta- infra'
4650 )
4751 } )
48-
49- it ( 'should not clone repository if insta-infra folder exists' , ( ) => {
50- jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true )
51- checkInstaInfraExists ( '/path/to/insta-infra' )
52- expect ( execSync ) . not . toHaveBeenCalled ( )
53- } )
5452} )
5553
5654describe ( 'runServices' , ( ) => {
57- it ( 'should run services successfully' , ( ) => {
55+ beforeEach ( ( ) => {
56+ jest . resetAllMocks ( )
57+ } )
58+
59+ it ( 'should run services successfully without persistence flag' , ( ) => {
5860 execSync . mockReturnValueOnce ( 'service1 service2' )
5961 execSync . mockReturnValueOnce ( 'success' )
60- runServices ( '/path/to/insta-infra' , [ 'service1' , 'service2' ] , {
61- ENV_VAR : 'value'
62- } )
63- expect ( execSync ) . toHaveBeenCalledWith ( './run.sh service1 service2' , {
64- cwd : '/path/to/insta-infra' ,
62+ runServices ( [ 'service1' , 'service2' ] , { ENV_VAR : 'value' } )
63+ // Verify services are started WITHOUT -p flag (no data persistence)
64+ expect ( execSync ) . toHaveBeenCalledWith ( 'insta service1 service2' , {
6565 stdio : 'pipe'
6666 } )
6767 } )
6868
6969 it ( 'should throw error if unsupported service is found' , ( ) => {
7070 execSync . mockReturnValueOnce ( 'service1\nservice2' )
71- expect ( ( ) =>
72- runServices (
73- '/path/to/insta-infra' ,
74- [ 'service1' , 'unsupportedService' ] ,
75- { }
76- )
77- ) . toThrow ( 'Unsupported service: unsupportedService' )
71+ expect ( ( ) => runServices ( [ 'service1' , 'unsupportedService' ] , { } ) ) . toThrow (
72+ 'Unsupported service: unsupportedService'
73+ )
7874 } )
7975
8076 it ( 'should log error and check container status if running services fail' , ( ) => {
@@ -83,9 +79,7 @@ describe('runServices', () => {
8379 throw new Error ( 'run failed' )
8480 } )
8581 isContainerFinished . mockReturnValueOnce ( true )
86- expect ( ( ) => runServices ( '/path/to/insta-infra' , [ 'service1' ] , { } ) ) . toThrow (
87- 'run failed'
88- )
82+ expect ( ( ) => runServices ( [ 'service1' ] , { } ) ) . toThrow ( 'run failed' )
8983 expect ( logger . logError ) . toHaveBeenCalledWith (
9084 '[Service]' ,
9185 'Failed to start services: service1'
@@ -98,17 +92,57 @@ describe('runServices', () => {
9892 throw new Error ( 'run failed' )
9993 } )
10094 isContainerFinished . mockReturnValue ( false )
101- expect ( ( ) =>
102- runServices (
103- '/path/to/insta-infra' ,
104- [ 'service1' , 'service2' , 'service3' ] ,
105- { }
106- )
107- ) . toThrow ( 'run failed' )
95+ expect ( ( ) => runServices ( [ 'service1' , 'service2' , 'service3' ] , { } ) ) . toThrow (
96+ 'run failed'
97+ )
10898 // All three services should be checked
10999 expect ( isContainerFinished ) . toHaveBeenCalledTimes ( 3 )
110100 expect ( isContainerFinished ) . toHaveBeenCalledWith ( 'service1' )
111101 expect ( isContainerFinished ) . toHaveBeenCalledWith ( 'service2' )
112102 expect ( isContainerFinished ) . toHaveBeenCalledWith ( 'service3' )
113103 } )
104+
105+ it ( 'should set environment variables before starting services' , ( ) => {
106+ execSync . mockReturnValueOnce ( 'postgres' )
107+ execSync . mockReturnValueOnce ( 'success' )
108+ const envVars = { POSTGRES_USER : 'test' , POSTGRES_PASSWORD : 'secret' }
109+ runServices ( [ 'postgres' ] , envVars )
110+ expect ( process . env . POSTGRES_USER ) . toBe ( 'test' )
111+ expect ( process . env . POSTGRES_PASSWORD ) . toBe ( 'secret' )
112+ } )
113+ } )
114+
115+ describe ( 'shutdownServices' , ( ) => {
116+ beforeEach ( ( ) => {
117+ jest . resetAllMocks ( )
118+ } )
119+
120+ it ( 'should shutdown specific services' , ( ) => {
121+ execSync . mockReturnValueOnce ( 'success' )
122+ shutdownServices ( [ 'postgres' , 'mysql' ] )
123+ expect ( execSync ) . toHaveBeenCalledWith ( 'insta -d postgres mysql' , {
124+ stdio : 'pipe'
125+ } )
126+ } )
127+
128+ it ( 'should shutdown all services when no services specified' , ( ) => {
129+ execSync . mockReturnValueOnce ( 'success' )
130+ shutdownServices ( )
131+ expect ( execSync ) . toHaveBeenCalledWith ( 'insta -d' , { stdio : 'pipe' } )
132+ } )
133+
134+ it ( 'should shutdown all services when empty array provided' , ( ) => {
135+ execSync . mockReturnValueOnce ( 'success' )
136+ shutdownServices ( [ ] )
137+ expect ( execSync ) . toHaveBeenCalledWith ( 'insta -d' , { stdio : 'pipe' } )
138+ } )
139+
140+ it ( 'should warn but not throw if shutdown fails' , ( ) => {
141+ execSync . mockImplementationOnce ( ( ) => {
142+ throw new Error ( 'shutdown failed' )
143+ } )
144+ // Should not throw
145+ expect ( ( ) => shutdownServices ( [ 'postgres' ] ) ) . not . toThrow ( )
146+ expect ( logger . warn ) . toHaveBeenCalled ( )
147+ } )
114148} )
0 commit comments