Coverage for src/meshadmin/cli/tests/test_context.py: 100%

62 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-22 07:26 +0200

1from typer.testing import CliRunner 

2 

3from meshadmin.cli.main import app 

4 

5runner = CliRunner() 

6 

7 

8def test_version(): 

9 result = runner.invoke(app, ["--version"]) 

10 assert result.exit_code == 0 

11 assert "meshadmin version" in result.stdout 

12 

13 

14def test_context_list_no_contexts(temp_config_dir): 

15 result = runner.invoke( 

16 app, ["--config-path", str(temp_config_dir), "context", "list"] 

17 ) 

18 assert result.exit_code == 0 

19 assert "No contexts found" in result.stdout 

20 

21 

22def test_context_create(temp_config_dir): 

23 result = runner.invoke( 

24 app, 

25 [ 

26 "--config-path", 

27 str(temp_config_dir), 

28 "context", 

29 "create", 

30 "test-context", 

31 "--endpoint", 

32 "http://localhost:8000", 

33 ], 

34 ) 

35 assert result.exit_code == 0 

36 assert "Created context 'test-context'" in result.stdout 

37 assert "Set 'test-context' as active context" in result.stdout 

38 

39 

40def test_context_switch(temp_config_dir): 

41 result = runner.invoke( 

42 app, 

43 [ 

44 "--config-path", 

45 str(temp_config_dir), 

46 "context", 

47 "create", 

48 "second-context", 

49 "--endpoint", 

50 "http://localhost:8001", 

51 ], 

52 ) 

53 assert result.exit_code == 0 

54 result = runner.invoke( 

55 app, ["--config-path", str(temp_config_dir), "context", "use", "second-context"] 

56 ) 

57 assert result.exit_code == 0 

58 assert "Switched to context 'second-context'" in result.stdout 

59 

60 

61def test_config_info(temp_config_dir, sample_context): 

62 result = runner.invoke( 

63 app, ["--config-path", str(temp_config_dir), "host", "config", "info"] 

64 ) 

65 assert result.exit_code == 0 

66 assert "Configuration Paths:" in result.stdout 

67 assert "Contexts file:" in result.stdout 

68 assert "test-context" in result.stdout 

69 assert "http://localhost:8000" in result.stdout 

70 assert "nebula1" in result.stdout 

71 

72 

73def test_invalid_context(temp_config_dir, sample_context): 

74 result = runner.invoke( 

75 app, 

76 [ 

77 "--config-path", 

78 str(temp_config_dir), 

79 "--context", 

80 "nonexistent", 

81 "host", 

82 "config", 

83 "info", 

84 ], 

85 ) 

86 assert result.exit_code == 1 

87 assert "Context 'nonexistent' not found" in result.stdout 

88 

89 

90def test_env_var_config_path(temp_config_dir, sample_context, monkeypatch): 

91 monkeypatch.setenv("MESHADMIN_CONFIG_PATH", str(temp_config_dir)) 

92 result = runner.invoke(app, ["host", "config", "info"]) 

93 assert result.exit_code == 0 

94 assert "Configuration Paths:" in result.stdout 

95 assert "Contexts file:" in result.stdout 

96 assert "test-context" in result.stdout 

97 assert "http://localhost:8000" in result.stdout 

98 

99 

100def test_env_var_context(temp_config_dir, sample_context, monkeypatch): 

101 runner.invoke( 

102 app, 

103 [ 

104 "--config-path", 

105 str(temp_config_dir), 

106 "context", 

107 "create", 

108 "second-context", 

109 "--endpoint", 

110 "http://localhost:8001", 

111 ], 

112 ) 

113 monkeypatch.setenv("MESH_CONTEXT", "second-context") 

114 result = runner.invoke( 

115 app, ["--config-path", str(temp_config_dir), "host", "config", "info"] 

116 ) 

117 assert result.exit_code == 0 

118 assert "second-context" in result.stdout 

119 

120 

121def test_context_switch_with_no_contexts(temp_config_dir): 

122 result = runner.invoke( 

123 app, ["--config-path", str(temp_config_dir), "context", "use", "second-context"] 

124 ) 

125 assert result.exit_code == 1 

126 assert "No contexts found" in result.stdout 

127 

128 

129def test_context_switch_with_nonexistent_context(temp_config_dir, sample_context): 

130 result = runner.invoke( 

131 app, ["--config-path", str(temp_config_dir), "context", "use", "nonexistent"] 

132 ) 

133 assert result.exit_code == 1 

134 assert "Context 'nonexistent' not found" in result.stdout 

135 

136 

137def test_list_contexts(temp_config_dir, sample_context): 

138 runner.invoke( 

139 app, 

140 [ 

141 "--config-path", 

142 str(temp_config_dir), 

143 "context", 

144 "create", 

145 "second-context", 

146 "--endpoint", 

147 "http://localhost:8001", 

148 ], 

149 ) 

150 result = runner.invoke( 

151 app, ["--config-path", str(temp_config_dir), "context", "list"] 

152 ) 

153 assert result.exit_code == 0 

154 assert "* test-context" in result.stdout 

155 assert "second-context" in result.stdout