Coverage for src/meshadmin/cli/test_cli.py: 100%

67 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-10 16:08 +0200

1import pytest 

2import yaml 

3from typer.testing import CliRunner 

4 

5from meshadmin.cli.main import app 

6 

7runner = CliRunner() 

8 

9 

10@pytest.fixture 

11def temp_config_dir(tmp_path): 

12 config_dir = tmp_path / "meshadmin" 

13 config_dir.mkdir() 

14 return config_dir 

15 

16 

17@pytest.fixture 

18def sample_context(temp_config_dir): 

19 contexts = { 

20 "test-context": { 

21 "endpoint": "http://localhost:8000", 

22 "interface": "nebula1", 

23 "active": True, 

24 } 

25 } 

26 contexts_file = temp_config_dir / "contexts.yaml" 

27 with open(contexts_file, "w") as f: 

28 yaml.dump(contexts, f) 

29 return contexts 

30 

31 

32def test_version(): 

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

34 assert result.exit_code == 0 

35 assert "meshadmin version" in result.stdout 

36 

37 

38def test_context_list_no_contexts(temp_config_dir): 

39 result = runner.invoke( 

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

41 ) 

42 assert result.exit_code == 0 

43 assert "No contexts found" in result.stdout 

44 

45 

46def test_context_create(temp_config_dir): 

47 result = runner.invoke( 

48 app, 

49 [ 

50 "--config-path", 

51 str(temp_config_dir), 

52 "context", 

53 "create", 

54 "test-context", 

55 "--endpoint", 

56 "http://localhost:8000", 

57 ], 

58 ) 

59 assert result.exit_code == 0 

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

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

62 

63 

64def test_context_switch(temp_config_dir, sample_context): 

65 result = runner.invoke( 

66 app, 

67 [ 

68 "--config-path", 

69 str(temp_config_dir), 

70 "context", 

71 "create", 

72 "second-context", 

73 "--endpoint", 

74 "http://localhost:8001", 

75 ], 

76 ) 

77 assert result.exit_code == 0 

78 result = runner.invoke( 

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

80 ) 

81 assert result.exit_code == 0 

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

83 

84 

85def test_context_flag_override(temp_config_dir, sample_context): 

86 runner.invoke( 

87 app, 

88 [ 

89 "--config-path", 

90 str(temp_config_dir), 

91 "context", 

92 "create", 

93 "second-context", 

94 "--endpoint", 

95 "http://localhost:8001", 

96 ], 

97 ) 

98 result = runner.invoke( 

99 app, 

100 [ 

101 "--config-path", 

102 str(temp_config_dir), 

103 "--context", 

104 "second-context", 

105 "host", 

106 "config", 

107 "info", 

108 ], 

109 ) 

110 assert result.exit_code == 0 

111 assert "second-context" in result.stdout 

112 

113 

114def test_config_info(temp_config_dir, sample_context): 

115 result = runner.invoke( 

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

117 ) 

118 assert result.exit_code == 0 

119 assert "Configuration Paths:" in result.stdout 

120 assert "Contexts file:" in result.stdout 

121 assert "test-context" in result.stdout 

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

123 assert "nebula1" in result.stdout 

124 

125 

126def test_invalid_context(temp_config_dir, sample_context): 

127 result = runner.invoke( 

128 app, 

129 [ 

130 "--config-path", 

131 str(temp_config_dir), 

132 "--context", 

133 "nonexistent", 

134 "host", 

135 "config", 

136 "info", 

137 ], 

138 ) 

139 assert result.exit_code == 1 

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

141 

142 

143def test_env_var_config_path(temp_config_dir, sample_context, monkeypatch): 

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

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

146 assert result.exit_code == 0 

147 assert "Configuration Paths:" in result.stdout 

148 assert "Contexts file:" in result.stdout 

149 assert "test-context" in result.stdout 

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

151 

152 

153def test_env_var_context(temp_config_dir, sample_context, monkeypatch): 

154 runner.invoke( 

155 app, 

156 [ 

157 "--config-path", 

158 str(temp_config_dir), 

159 "context", 

160 "create", 

161 "second-context", 

162 "--endpoint", 

163 "http://localhost:8001", 

164 ], 

165 ) 

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

167 result = runner.invoke( 

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

169 ) 

170 assert result.exit_code == 0 

171 assert "second-context" in result.stdout