def test_delete_single_cluster(self):
     # Create in cluster #1
     token_name = self.token_name()
     version_1 = str(uuid.uuid4())
     util.post_token(self.waiter_url_1, token_name, {'version': version_1})
     try:
         # Create in cluster #2
         version_2 = str(uuid.uuid4())
         util.post_token(self.waiter_url_2, token_name, {'version': version_2})
         try:
             config = self.__two_cluster_config()
             with cli.temp_config_file(config) as path:
                 # Delete the token in one cluster only
                 cp = cli.delete(token_name=token_name, flags=f'--config {path} --cluster waiter2')
                 self.assertEqual(0, cp.returncode, cp.stderr)
                 self.assertNotIn('exists in 2 clusters', cli.stdout(cp))
                 self.assertNotIn('waiter1', cli.stdout(cp))
                 self.assertIn('waiter2', cli.stdout(cp))
                 self.assertEqual(1, cli.stdout(cp).count('Deleting token'))
                 self.assertEqual(1, cli.stdout(cp).count('Successfully deleted'))
                 util.load_token(self.waiter_url_1, token_name, expected_status_code=200)
                 util.load_token(self.waiter_url_2, token_name, expected_status_code=404)
         finally:
             util.delete_token(self.waiter_url_2, token_name, assert_response=False)
     finally:
         util.delete_token(self.waiter_url_1, token_name, assert_response=True)
    def test_ping_via_token_cluster(self):
        # Create in cluster #1
        token_name = self.token_name()
        token_data = util.minimal_service_description()
        util.post_token(self.waiter_url_1, token_name, token_data)
        try:
            # Create in cluster #2
            token_data['cluster'] = util.load_token(self.waiter_url_1, token_name)['cluster']
            util.post_token(self.waiter_url_2, token_name, token_data)
            try:
                config = self.__two_cluster_config()
                with cli.temp_config_file(config) as path:
                    # Ping the token, which should only ping in cluster #1
                    cp = cli.ping(token_name_or_service_id=token_name, flags=f'--config {path}')
                    self.assertEqual(0, cp.returncode, cp.stderr)
                    self.assertIn('waiter1', cli.stdout(cp))
                    self.assertEqual(1, cli.stdout(cp).count('Pinging token'))
                    self.assertEqual(1, cli.stdout(cp).count('Not pinging token'))
                    self.assertEqual(1, len(util.services_for_token(self.waiter_url_1, token_name)))
                    self.assertEqual(0, len(util.services_for_token(self.waiter_url_2, token_name)))

                    # Ping the token in cluster #2 explicitly
                    cp = cli.ping(token_name_or_service_id=token_name, flags=f'--config {path} --cluster waiter2')
                    self.assertEqual(0, cp.returncode, cp.stderr)
                    self.assertIn('waiter2', cli.stdout(cp))
                    self.assertEqual(1, cli.stdout(cp).count('Pinging token'))
                    self.assertEqual(0, cli.stdout(cp).count('Not pinging token'))
                    self.assertEqual(1, len(util.services_for_token(self.waiter_url_2, token_name)))
            finally:
                util.delete_token(self.waiter_url_2, token_name, kill_services=True)
        finally:
            util.delete_token(self.waiter_url_1, token_name, kill_services=True)
    def test_update_non_default_cluster(self):
        # Set up the config so that cluster #1 is the default
        config = {'clusters': [{'name': 'waiter1', 'url': self.waiter_url_1, 'default-for-create': True},
                               {'name': 'waiter2', 'url': self.waiter_url_2}]}

        # Create in cluster #2 (the non-default)
        token_name = self.token_name()
        service_description = util.minimal_service_description()
        util.post_token(self.waiter_url_2, token_name, service_description)
        try:
            # Update using the CLI, which should update in cluster #2
            with cli.temp_config_file(config) as path:
                version = str(uuid.uuid4())
                cp = cli.update(token_name=token_name, flags=f'--config {path}', update_flags=f'--version {version}')
                self.assertEqual(0, cp.returncode, cp.stderr)
                self.assertNotIn('waiter1', cli.stdout(cp))
                self.assertIn('waiter2', cli.stdout(cp))
                token_1 = util.load_token(self.waiter_url_1, token_name, expected_status_code=404)
                token_2 = util.load_token(self.waiter_url_2, token_name, expected_status_code=200)
                self.assertNotIn('version', token_1)
                self.assertEqual(version, token_2['version'])
        finally:
            util.delete_token(self.waiter_url_2, token_name)