1# Licensed to the Apache Software Foundation (ASF) under one or more 2# contributor license agreements. See the NOTICE file distributed with 3# this work for additional information regarding copyright ownership. 4# The ASF licenses this file to You under the Apache License, Version 2.0 5# (the "License"); you may not use this file except in compliance with 6# the License. You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15""" 16Tests for Google Container Engine Driver 17""" 18 19import sys 20import unittest 21 22from libcloud.utils.py3 import httplib 23from libcloud.container.drivers.gke import GKEContainerDriver, API_VERSION 24from libcloud.common.google import (GoogleBaseAuthConnection) 25from libcloud.test.common.test_google import GoogleAuthMockHttp, GoogleTestCase 26 27from libcloud.test import MockHttp 28from libcloud.test.container import TestCaseMixin 29from libcloud.test.file_fixtures import ContainerFileFixtures 30 31from libcloud.test.secrets import GKE_PARAMS, GKE_KEYWORD_PARAMS 32 33 34class GKEContainerDriverTestCase(GoogleTestCase, TestCaseMixin): 35 """ 36 Google Compute Engine Test Class. 37 """ 38 # Mock out a few specific calls that interact with the user, system or 39 # environment. 40 datacenter = 'us-central1-a' 41 42 def setUp(self): 43 GKEMockHttp.test = self 44 GKEContainerDriver.connectionCls.conn_class = GKEMockHttp 45 GoogleBaseAuthConnection.conn_class = GoogleAuthMockHttp 46 GKEMockHttp.type = None 47 kwargs = GKE_KEYWORD_PARAMS.copy() 48 kwargs['auth_type'] = 'IA' 49 kwargs['datacenter'] = self.datacenter 50 self.driver = GKEContainerDriver(*GKE_PARAMS, **kwargs) 51 52 def test_list_images_response(self): 53 config = self.driver.list_clusters(ex_zone="us-central1-a") 54 assert "clusters" in config 55 assert config["clusters"][0]["zone"] == "us-central1-a" 56 57 def test_server_config(self): 58 config = self.driver.get_server_config() 59 assert "validImageTypes" in config 60 61 62class GKEMockHttp(MockHttp): 63 fixtures = ContainerFileFixtures('gke') 64 json_hdr = {'content-type': 'application/json; charset=UTF-8'} 65 66 def _get_method_name(self, type, use_param, qs, path): 67 api_path = '/%s' % API_VERSION 68 project_path = '/projects/%s' % GKE_KEYWORD_PARAMS['project'] 69 path = path.replace(api_path, '') 70 # This replace is separate, since there is a call with a different 71 # project name 72 path = path.replace(project_path, '') 73 # The path to get project information is the base path, so use a fake 74 # '/project' path instead 75 if not path: 76 path = '/project' 77 method_name = super(GKEMockHttp, self)._get_method_name( 78 type, use_param, qs, path) 79 return method_name 80 81 def _zones_us_central1_a_serverconfig(self, method, url, body, headers): 82 body = self.fixtures.load( 83 'zones_us-central1-a_instance_serverconfig.json') 84 return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) 85 86 def _zones_us_central1_a_clusters(self, method, url, body, headers): 87 body = self.fixtures.load( 88 'zones_us-central1-a_list.json') 89 return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) 90 91 92if __name__ == '__main__': 93 sys.exit(unittest.main()) 94