Skip to content

Commit

Permalink
add adapters and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthyx authored and manusa committed May 20, 2021
1 parent 77949b6 commit 95e8fde
Show file tree
Hide file tree
Showing 12 changed files with 574 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ public Boolean isAdaptable(Client client) {
public CertManagerClient adapt(Client client) {
return new DefaultCertManagerClient(client.adapt(OkHttpClient.class), client.getConfiguration());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.certmanager.client;

import io.fabric8.kubernetes.client.APIGroupExtensionAdapter;
import io.fabric8.kubernetes.client.Client;
import okhttp3.OkHttpClient;

public class V1APIGroupExtensionAdapter extends APIGroupExtensionAdapter<V1APIGroupClient> {
@Override
protected String getAPIGroupName() {
return "v1";
}

@Override
public Class<V1APIGroupClient> getExtensionType() {
return V1APIGroupClient.class;
}

@Override
protected V1APIGroupClient newInstance(Client client) {
return new V1APIGroupClient(client.adapt(OkHttpClient.class), client.getConfiguration());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.certmanager.client;

import io.fabric8.kubernetes.client.APIGroupExtensionAdapter;
import io.fabric8.kubernetes.client.Client;
import okhttp3.OkHttpClient;

public class V1alpha2APIGroupExtensionAdapter extends APIGroupExtensionAdapter<V1alpha2APIGroupClient> {
@Override
protected String getAPIGroupName() {
return "v1alpha2";
}

@Override
public Class<V1alpha2APIGroupClient> getExtensionType() {
return V1alpha2APIGroupClient.class;
}

@Override
protected V1alpha2APIGroupClient newInstance(Client client) {
return new V1alpha2APIGroupClient(client.adapt(OkHttpClient.class), client.getConfiguration());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.certmanager.client;

import io.fabric8.kubernetes.client.APIGroupExtensionAdapter;
import io.fabric8.kubernetes.client.Client;
import okhttp3.OkHttpClient;

public class V1alpha3APIGroupExtensionAdapter extends APIGroupExtensionAdapter<V1alpha3APIGroupClient> {
@Override
protected String getAPIGroupName() {
return "v1alpha3";
}

@Override
public Class<V1alpha3APIGroupClient> getExtensionType() {
return V1alpha3APIGroupClient.class;
}

@Override
protected V1alpha3APIGroupClient newInstance(Client client) {
return new V1alpha3APIGroupClient(client.adapt(OkHttpClient.class), client.getConfiguration());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.certmanager.client;

import io.fabric8.kubernetes.client.APIGroupExtensionAdapter;
import io.fabric8.kubernetes.client.Client;
import okhttp3.OkHttpClient;

public class V1beta1APIGroupExtensionAdapter extends APIGroupExtensionAdapter<V1beta1APIGroupClient> {
@Override
protected String getAPIGroupName() {
return "v1beta1";
}

@Override
public Class<V1beta1APIGroupClient> getExtensionType() {
return V1beta1APIGroupClient.class;
}

@Override
protected V1beta1APIGroupClient newInstance(Client client) {
return new V1beta1APIGroupClient(client.adapt(OkHttpClient.class), client.getConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
#

io.fabric8.certmanager.client.CertManagerExtensionAdapter
io.fabric8.certmanager.client.V1alpha2APIGroupExtensionAdapter
io.fabric8.certmanager.client.V1alpha3APIGroupExtensionAdapter
io.fabric8.certmanager.client.V1beta1APIGroupExtensionAdapter
io.fabric8.certmanager.client.V1APIGroupExtensionAdapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.certmanager.examples;

import io.fabric8.certmanager.client.CertManagerClient;
import io.fabric8.certmanager.client.DefaultCertManagerClient;
import io.fabric8.kubernetes.client.ConfigBuilder;

public class ClientFactory {

public static CertManagerClient newClient(String[] args) {
ConfigBuilder config = new ConfigBuilder();
for (int i = 0; i < args.length - 1; i++) {

String key = args[i];
String value = args[i + 1];

if (key.equals("--api-server")) {
config = config.withMasterUrl(value);
}

if (key.equals("--token")) {
config = config.withOauthToken(value);
}

if (key.equals("--username")) {
config = config.withUsername(value);
}

if (key.equals("--password")) {
config = config.withPassword(value);
}
if (key.equals("--namespace")) {
config = config.withNamespace(value);
}
}
return new DefaultCertManagerClient(config.build());
}

public static String getOptions(String args[], String name, String defaultValue) {
for (int i = 0; i < args.length - 1; i++) {
String key = args[i];
String value = args[i + 1];
if (key.equals(name)) {
return value;
}
}
return defaultValue;
}
}

6 changes: 6 additions & 0 deletions extensions/certmanager/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>tekton-mock</artifactId>
<version>5.4-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.certmanager.test.crud;

import io.fabric8.certmanager.api.model.v1.Certificate;
import io.fabric8.certmanager.api.model.v1.CertificateBuilder;
import io.fabric8.certmanager.api.model.v1.CertificateList;
import io.fabric8.certmanager.client.CertManagerClient;
import io.fabric8.certmanager.server.mock.CertManagerServer;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.io.ByteArrayInputStream;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

@EnableRuleMigrationSupport
class V1CertificateCrudTest {

@Rule
public CertManagerServer server = new CertManagerServer(true, true);

@Test
void shouldReturnEmptyList() {
CertManagerClient client = server.get();
CertificateList certificateList = client.v1().certificates().inNamespace("ns1").list();
assertNotNull(certificateList);
assertTrue(certificateList.getItems().isEmpty());
}

@Test
void shouldListAndGetCertificate() {
CertManagerClient client = server.get();
Certificate certificate2 = new CertificateBuilder().withNewMetadata().withName("cert2").endMetadata().build();
client.v1().certificates().inNamespace("ns2").create(certificate2);
CertificateList certificateList = client.v1().certificates().inNamespace("ns2").list();
assertNotNull(certificateList);
assertEquals(1, certificateList.getItems().size());
}

@Test
void shouldDeleteACertificate() {
CertManagerClient client = server.get();
Certificate certificate3 = new CertificateBuilder().withNewMetadata().withName("cert3").endMetadata().build();
client.v1().certificates().inNamespace("ns3").create(certificate3);
Boolean deleted = client.v1().certificates().inNamespace("ns3").withName("cert3").delete();
assertTrue(deleted);
}

@Test
void shouldLoadCertificate() {
CertManagerClient client = server.get();
String certificateDefinition = String.join("\n", Arrays.asList(
"apiVersion: cert-manager.io/v1beta1",
"kind: Certificate",
"metadata:",
" name: ca-issuer",
"spec:",
" isCA: true",
" secretName: ca-key-pair",
" commonName: my-csi-app",
" issuerRef:",
" name: selfsigned-issuer",
" kind: Issuer",
" group: cert-manager.io"
));
io.fabric8.certmanager.api.model.v1beta1.Certificate certificate = client.v1beta1().certificates().inNamespace("ns4").load(new ByteArrayInputStream(certificateDefinition.getBytes())).createOrReplace();
assertEquals("ca-issuer", certificate.getMetadata().getName());
assertEquals("ca-key-pair", certificate.getSpec().getSecretName());
assertEquals("my-csi-app", certificate.getSpec().getCommonName());
}

}
Loading

0 comments on commit 95e8fde

Please sign in to comment.