Skip to content

Commit

Permalink
Merge pull request juju#18723 from CodingCookieRookie/test-empty-cred…
Browse files Browse the repository at this point in the history
…entials-ddl-support

juju#18723

This PR enhances the test coverage for scenarios involving empty credentials in the model handling logic. The v_model view build a lot of information about a model’s credential that is done with a left join. We need a test to assert that when the credential is null in the database the model and it’s credential information correctly come back out of the v_model view..

## Checklist

~~- [ ] Code style: imports ordered, good names, simple structure, etc~~
~~- [ ] Comments saying why design decisions were made~~
~~- [ ] [Integration tests](https://github.com/juju/juju/tree/main/tests), with comments saying what you're testing~~
~~- [ ] [doc.go](https://discourse.charmhub.io/t/readme-in-packages/451) added or updated in changed packages~~
- [x] Go unit tests, with comments saying what you're testing


## Unit test steps
 1. From project directory, go to domain/model/state folder
```sh
cd domain/model/state
```

 2. Run unit tests in the directory
```sh
go test -v .
```

## QA steps
1. Finds ubuntu AMI

```sh
aws ec2 describe-images \n --owners 099720109477 \n --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-?????-amd64-server-????????" 'Name=state,Values=available' \n --query 'reverse(sort_by(Images, &CreationDate))[:1].ImageId' --output text

instance_image_id=ami-0e1bed4f06a3b463d
```


2. Create VPC
```sh
aws ec2 create-vpc --cidr-block 10.0.0.0/28 \n --query 'Vpc.VpcId' --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=manual-deploy}]' --output text

vpc_id = vpc-0432dbe7ec058d2c0
```

3. Creates internet gateway
```sh
aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text

igw_id=igw-0d37d1d8126377401
```

4. Attach internet gateway with vac
```sh
aws ec2 attach-internet-gateway --internet-gateway-id "${igw_id}" --vpc-id "${vpc_id}"
```

5. Create subnet

```sh
aws ec2 create-subnet --vpc-id "${vpc_id}" --cidr-block 10.0.0.0/28 --query 'Subnet.SubnetId' --output text

subnet_id=subnet-0914fd19b516578fb
```

6. Create security group
```sh
aws ec2 create-security-group --group-name "ci-manual-deploy" --description "run_deploy_manual_aws" --vpc-id "${vpc_id}" --query 'GroupId' --output text

sg_id=sg-07061ccc27117a995
```

7. Run instance

```sh
aws ec2 run-instances --image-id "${instance_image_id}" --count 1 --instance-type t2.medium --associate-public-ip-address --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=alvincontroller}]" --key-name "testkeypair2" --subnet-id "${subnet_id}" --security-group-ids "${sg_id}" --query 'Instances[0].InstanceId' --output text


instance_id=i-03c45a50f959c29b4
```

8. Query instance for public ip 
```sh
aws ec2 describe-instances --instance-ids "${instance_id}" \n --query 'Reservations[0].Instances[0].PublicIpAddress' \n --output text
```
Output: 3.93.217.117

9. Add a yaml file for manual cloud in user directory
```sh
cd ~
echo "clouds:
 alvin-cloud:
 type: manual
 endpoint: [email protected]
 regions:
 default:
 endpoint: [email protected]" > mycloud.yaml
```

10. Bootstrap a manual controller
```sh
juju bootstrap alvin-cloud test-alvin
```

12. Check credential information for manual controller in dqlite
```sh
juju ssh 0
sudo /var/lib/juju/tools/machine-0/jujud db-repl --machine-id 0
select prdesc from model
```

## Links

**Jira card:** https://warthogs.atlassian.net/browse/JUJU-7395
  • Loading branch information
jujubot authored Feb 3, 2025
2 parents d549a6d + 1dc8756 commit b65dd7e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions domain/model/state/controllerstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,68 @@ UPDATE model SET life_id = 1 WHERE uuid = ?
})
}

// TestGetEmptyCredentialsModel verifies that the model view continues to work correctly
// when the model's credentials are empty. This ensures that even in cases where a model
// is created without associated credentials (e.g., nil credential scenarios), the system
// behaves as expected.
//
// Specifically, the test validates that (for both IAAS and CAAS model types):
// 1. A model does not encounter errors when created without credentials.
// 2. Activating such a model does not cause unexpected errors.
// 3. Retrieving the model's state and properties works as expected even when credentials
// are missing.
//
// This test addresses potential issues with SQL queries that include joins involving credential data.
// With this test, we can ensure that future modifications to the SQL layer do not unintentionally
// break functionality, preserving accessibility and consistent behavior for models with nil credentials.
func (m *stateSuite) TestGetEmptyCredentialsModel(c *gc.C) {
// Define the test cases for different model types
testCases := []struct {
modelType coremodel.ModelType
modelName string
}{
{
modelType: coremodel.IAAS,
modelName: "my-iaas-model",
},
{
modelType: coremodel.CAAS,
modelName: "my-container-model",
},
}

for _, test := range testCases {
modelState := NewState(m.TxnRunnerFactory())
modelUUID := modeltesting.GenModelUUID(c)

// Create model with empty credentials
modelCreationArgs := model.GlobalModelCreationArgs{
Cloud: "my-cloud",
CloudRegion: "my-region",
Name: test.modelName,
Owner: m.userUUID,
SecretBackend: juju.BackendName,
}

err := modelState.Create(context.Background(), modelUUID, test.modelType, modelCreationArgs)
c.Assert(err, jc.ErrorIsNil)

err = modelState.Activate(context.Background(), modelUUID)
c.Assert(err, jc.ErrorIsNil)

retrievedModel, err := modelState.GetModel(context.Background(), modelUUID)
c.Assert(err, jc.ErrorIsNil)
c.Assert(retrievedModel, gc.NotNil)

c.Check(retrievedModel.AgentVersion, jc.DeepEquals, modelCreationArgs.AgentVersion)
c.Check(retrievedModel.Cloud, gc.Equals, modelCreationArgs.Cloud)
c.Check(retrievedModel.CloudRegion, gc.Equals, modelCreationArgs.CloudRegion)
c.Check(retrievedModel.Credential, jc.DeepEquals, modelCreationArgs.Credential)
c.Check(retrievedModel.Name, gc.Equals, modelCreationArgs.Name)
c.Check(retrievedModel.Owner, jc.DeepEquals, modelCreationArgs.Owner)
}
}

// createSuperuser adds a new superuser created by themselves.
func (m *stateSuite) createSuperuser(c *gc.C, accessState *accessstate.State, name user.Name) user.UUID {
userUUID, err := user.NewUUID()
Expand Down

0 comments on commit b65dd7e

Please sign in to comment.