-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add authz to hosts endpoints #1617
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBEnvironDAOImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* Copyright (c) 2024 Pinterest, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.pinterest.deployservice.db; | ||
|
||
import static org.junit.Assert.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.Instant; | ||
|
||
import com.pinterest.deployservice.bean.BeanUtils; | ||
import com.pinterest.deployservice.bean.EnvironBean; | ||
import com.pinterest.deployservice.bean.HostAgentBean; | ||
import com.pinterest.deployservice.bean.HostBean; | ||
import com.pinterest.deployservice.dao.EnvironDAO; | ||
import com.pinterest.deployservice.dao.HostAgentDAO; | ||
import com.pinterest.deployservice.dao.HostDAO; | ||
import com.pinterest.deployservice.fixture.EnvironBeanFixture; | ||
import org.apache.commons.dbcp.BasicDataSource; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DBEnvironDAOImplTest { | ||
private static final String HOST_ID = "host123"; | ||
private static final String TEST_CLUSTER = "test-cluster"; | ||
private static BasicDataSource dataSource; | ||
private static HostAgentDAO hostAgentDAO; | ||
private static HostDAO hostDAO; | ||
private EnvironDAO sut; | ||
|
||
@BeforeAll | ||
static void setUpAll() throws Exception { | ||
dataSource = DBUtils.createTestDataSource(); | ||
hostAgentDAO = new DBHostAgentDAOImpl(dataSource); | ||
hostDAO = new DBHostDAOImpl(dataSource); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
sut = new DBEnvironDAOImpl(dataSource); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws Exception { | ||
DBUtils.truncateAllTables(dataSource); | ||
} | ||
|
||
@Test | ||
void testGetMainEnvByHostId_happyPath() throws Exception { | ||
EnvironBean expectedEnvBean = EnvironBeanFixture.createRandomEnvironBean(); | ||
expectedEnvBean.setCluster_name(TEST_CLUSTER); | ||
sut.insert(expectedEnvBean); | ||
|
||
HostAgentBean hostAgentBean = new HostAgentBean(); | ||
hostAgentBean.setHost_id(HOST_ID); | ||
hostAgentBean.setAuto_scaling_group(TEST_CLUSTER); | ||
hostAgentDAO.insert(hostAgentBean); | ||
|
||
HostBean hostBean = BeanUtils.createHostBean(Instant.now()); | ||
hostBean.setHost_id(HOST_ID); | ||
hostBean.setGroup_name(TEST_CLUSTER + "sidecar"); | ||
hostDAO.insert(hostBean); | ||
|
||
EnvironBean actualEnvironBean = sut.getMainEnvByHostId(HOST_ID); | ||
assertEquals(expectedEnvBean.getEnv_name(), actualEnvironBean.getEnv_name()); | ||
assertEquals(expectedEnvBean.getStage_name(), actualEnvironBean.getStage_name()); | ||
assertEquals(TEST_CLUSTER, actualEnvironBean.getCluster_name()); | ||
|
||
EnvironBean nullEnvironBean = sut.getMainEnvByHostId("random-host-id"); | ||
assertNull(nullEnvironBean); | ||
} | ||
|
||
@Test | ||
void testGetMainEnvByHostId_noHost() throws Exception { | ||
EnvironBean actualEnvironBean = sut.getMainEnvByHostId(HOST_ID); | ||
assertNull(actualEnvironBean); | ||
} | ||
|
||
@Test | ||
void testGetMainEnvByHostId_noEnv() throws Exception { | ||
HostAgentBean hostAgentBean = new HostAgentBean(); | ||
hostAgentBean.setHost_id(HOST_ID); | ||
hostAgentBean.setAuto_scaling_group(TEST_CLUSTER); | ||
hostAgentDAO.insert(hostAgentBean); | ||
|
||
EnvironBean actualEnvironBean = sut.getMainEnvByHostId(HOST_ID); | ||
assertNull(actualEnvironBean); | ||
} | ||
|
||
@Test | ||
void testGetMainEnvByHostId_noHostAgent() throws Exception { | ||
EnvironBean expectedEnvBean = EnvironBeanFixture.createRandomEnvironBean(); | ||
expectedEnvBean.setCluster_name(TEST_CLUSTER); | ||
sut.insert(expectedEnvBean); | ||
|
||
HostBean hostBean = BeanUtils.createHostBean(Instant.now()); | ||
hostBean.setHost_id(HOST_ID); | ||
hostBean.setGroup_name(TEST_CLUSTER); | ||
hostDAO.insert(hostBean); | ||
|
||
HostBean hostBean2 = BeanUtils.createHostBean(Instant.now()); | ||
hostBean.setHost_id(HOST_ID); | ||
hostBean.setGroup_name(TEST_CLUSTER + "2"); | ||
hostDAO.insert(hostBean2); | ||
|
||
EnvironBean actualEnvironBean = sut.getMainEnvByHostId(HOST_ID); | ||
assertEquals(expectedEnvBean.getEnv_name(), actualEnvironBean.getEnv_name()); | ||
assertEquals(expectedEnvBean.getStage_name(), actualEnvironBean.getStage_name()); | ||
assertEquals(TEST_CLUSTER, actualEnvironBean.getCluster_name()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a
LIMIT 1
on the outer query as well? or do extra rows just get ignored so it does not matter?Also, I'm thinking the subquery could be removed if we had an outer
LIMIT 1
and the first select was ordered before the second select. Might not make much difference.LGTM overall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
NOT EXISTS
clause that checks whether the first query returns any results. If the first query returns results, theNOT EXISTS
clause will be false, and the second query will not return any results. If the first query does not return any results, theNOT EXISTS
clause will be true, and the second query will return its results.We use BeanHandler so it will take the first result. Although we don't have unique key constraint on the cluster_name column, we don't have meaningful duplications